Skip to content

Commit

Permalink
feat: 메세지 생성 성공 시에만 id를 로깅하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
yunuo46 committed Dec 12, 2023
1 parent 7292afe commit d05a5c2
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions back/src/common/interceptors/ip-log.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler
CallHandler,
HttpStatus
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { Observable, map } from 'rxjs';
import * as winston from 'winston';
import * as DailyRotateFile from 'winston-daily-rotate-file';

Expand All @@ -29,15 +30,20 @@ export class IPLoggerInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const request = context.switchToHttp().getRequest();
const clientIp = request.headers['x-forwarded-for'] || request.ip;
const message = request.body.content;
const logData = {
level: 'info',
clientIp,
message,
timestamp: new Date().toISOString()
clientIp
};

this.logger.info(logData);
return next.handle();
const response = context.switchToHttp().getResponse();
return next.handle().pipe(
map(data => {
if (response.statusCode === HttpStatus.CREATED) {
const id = data.id;
this.logger.info({ ...logData, id });
}
return data;
})
);
}
}

0 comments on commit d05a5c2

Please sign in to comment.