-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from boostcampwm-2024/Feature/be/logger
[Feature] 로깅 인터셉터와 필터 적용
- Loading branch information
Showing
7 changed files
with
159 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Backend/apps/api/src/common/filters/http-exception.filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
ExceptionFilter, | ||
Catch, | ||
ArgumentsHost, | ||
HttpException, | ||
HttpStatus, | ||
Inject, | ||
} from '@nestjs/common'; | ||
import { Request, Response } from 'express'; | ||
import { Logger } from 'winston'; | ||
import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; | ||
|
||
@Catch() | ||
export class HttpExceptionFilter implements ExceptionFilter { | ||
constructor( | ||
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger, | ||
) {} | ||
|
||
catch(exception: unknown, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
|
||
const request = ctx.getRequest<Request>(); | ||
const response = ctx.getResponse<Response>(); | ||
|
||
const status = | ||
exception instanceof HttpException | ||
? exception.getStatus() | ||
: HttpStatus.INTERNAL_SERVER_ERROR; | ||
|
||
const message = | ||
exception instanceof HttpException | ||
? exception.getResponse() | ||
: exception; | ||
|
||
this.logger.error( | ||
`HTTP Status: ${status} Error Message: ${JSON.stringify(message)}`, | ||
); | ||
|
||
response.status(status).json({ | ||
statusCode: status, | ||
timestamp: new Date().toISOString(), | ||
path: request.url, | ||
}); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
Backend/apps/api/src/common/interceptors/logging.interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
CallHandler, | ||
ExecutionContext, | ||
Injectable, | ||
NestInterceptor, | ||
Inject, | ||
} from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { tap } from 'rxjs/operators'; | ||
import { Request, Response } from 'express'; | ||
import { Logger } from 'winston'; | ||
import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; | ||
|
||
@Injectable() | ||
export class LoggingInterceptor implements NestInterceptor { | ||
constructor( | ||
@Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger, | ||
) {} | ||
|
||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
const now = Date.now(); | ||
|
||
const ctx = context.switchToHttp(); | ||
const request = ctx.getRequest<Request>(); | ||
const response = ctx.getResponse<Response>(); | ||
|
||
const { method, originalUrl } = request; | ||
const headers = { ...request.headers }; | ||
delete headers['authorization']; // Authorization 헤더 제외 - 보안상 | ||
|
||
return next.handle().pipe( | ||
tap(() => { | ||
const statusCode = response.statusCode; | ||
const contentLength = response.get('content-length') || '0'; | ||
|
||
this.logger.info( | ||
`${method} ${originalUrl} ${statusCode} ${contentLength} - ${ | ||
Date.now() - now | ||
}ms`, | ||
{ headers }, // 제외한 헤더를 로그에 포함 | ||
); | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,26 @@ | ||
import * as winston from 'winston'; | ||
import 'winston-daily-rotate-file'; | ||
|
||
const logLevel = 'warn'; | ||
const logDir = './logs'; | ||
|
||
export const winstonConfig: winston.LoggerOptions = { | ||
level: 'error', // 'error' 레벨 이상의 로그만 출력 | ||
level: logLevel, | ||
format: winston.format.combine( | ||
winston.format.timestamp(), | ||
winston.format.printf(({ timestamp, level, message, stack }) => { | ||
return `${timestamp} [${level.toUpperCase()}]: ${message} ${ | ||
stack ? '\n' + stack : '' | ||
}`; | ||
winston.format.printf(({ timestamp, level, message }) => { | ||
return `${timestamp} [${level.toUpperCase()}]: ${message}`; | ||
}), | ||
), | ||
transports: [ | ||
new winston.transports.Console(), | ||
new winston.transports.DailyRotateFile({ | ||
dirname: logDir, | ||
filename: 'application-%DATE%.log', | ||
datePattern: 'YYYY-MM-DD', | ||
zippedArchive: true, | ||
maxSize: '20m', | ||
maxFiles: '14d', | ||
}), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters