Skip to content

Commit

Permalink
Merge pull request #238 from lidofinance/develop
Browse files Browse the repository at this point in the history
Develop to main
  • Loading branch information
Jeday authored Aug 15, 2024
2 parents a0c6e53 + ba53fe8 commit da97145
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/http/common/middleware/logger.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { Inject, Injectable, LoggerService, NestMiddleware } from '@nestjs/common';
import { LOGGER_PROVIDER } from '@lido-nestjs/logger';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { Request, Reply } from './interfaces';
import { FastifyRequest } from 'fastify';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
constructor(@Inject(LOGGER_PROVIDER) protected readonly logger: LoggerService) {}
constructor(
@Inject(WINSTON_MODULE_NEST_PROVIDER)
private readonly logger: LoggerService,
) {}

use(request: Request, reply: Reply, next: () => void) {
use(request: any, reply: Reply, next: () => void) {
const { ip, method, headers, originalUrl } = request;
const userAgent = headers['user-agent'] ?? '';

const ips = request.ips ? request.ips : [];

reply.on('finish', () => {
const { statusCode } = reply;
const log = { method, originalUrl, statusCode, userAgent, ip };
const log = { method, originalUrl, statusCode, userAgent, ip, ips };

this.logger.log('Query', log);
this.logger.log(JSON.stringify(log));
});

next();
Expand Down
2 changes: 1 addition & 1 deletion src/http/common/throttler/throttler.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { FastifyRequest } from 'fastify';
@Injectable()
export class ThrottlerBehindProxyGuard extends ThrottlerGuard {
protected async getTracker(request: FastifyRequest) {
return request.ips?.length ? request.ips[0] : request.ip;
return request.ip;
}
}

0 comments on commit da97145

Please sign in to comment.