로그출력
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
|||||||
ArgumentsHost,
|
ArgumentsHost,
|
||||||
HttpException,
|
HttpException,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
|
Logger,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
|
|
||||||
@@ -13,17 +14,11 @@ import { Request, Response } from 'express';
|
|||||||
* @description
|
* @description
|
||||||
* HTTP 예외뿐만 아니라 모든 예외를 잡아서 처리합니다.
|
* HTTP 예외뿐만 아니라 모든 예외를 잡아서 처리합니다.
|
||||||
* 예상치 못한 에러도 일관된 형식으로 응답합니다.
|
* 예상치 못한 에러도 일관된 형식으로 응답합니다.
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* // main.ts에서 전역 적용
|
|
||||||
* app.useGlobalFilters(new AllExceptionsFilter());
|
|
||||||
*
|
|
||||||
* @export
|
|
||||||
* @class AllExceptionsFilter
|
|
||||||
* @implements {ExceptionFilter}
|
|
||||||
*/
|
*/
|
||||||
@Catch()
|
@Catch()
|
||||||
export class AllExceptionsFilter implements ExceptionFilter {
|
export class AllExceptionsFilter implements ExceptionFilter {
|
||||||
|
private readonly logger = new Logger('ExceptionFilter');
|
||||||
|
|
||||||
catch(exception: unknown, host: ArgumentsHost) {
|
catch(exception: unknown, host: ArgumentsHost) {
|
||||||
const ctx = host.switchToHttp();
|
const ctx = host.switchToHttp();
|
||||||
const response = ctx.getResponse<Response>();
|
const response = ctx.getResponse<Response>();
|
||||||
@@ -33,7 +28,6 @@ export class AllExceptionsFilter implements ExceptionFilter {
|
|||||||
let message: string | string[];
|
let message: string | string[];
|
||||||
|
|
||||||
if (exception instanceof HttpException) {
|
if (exception instanceof HttpException) {
|
||||||
// HTTP 예외 처리
|
|
||||||
status = exception.getStatus();
|
status = exception.getStatus();
|
||||||
const exceptionResponse = exception.getResponse();
|
const exceptionResponse = exception.getResponse();
|
||||||
|
|
||||||
@@ -45,12 +39,10 @@ export class AllExceptionsFilter implements ExceptionFilter {
|
|||||||
message = exception.message;
|
message = exception.message;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 예상치 못한 에러 처리
|
|
||||||
status = HttpStatus.INTERNAL_SERVER_ERROR;
|
status = HttpStatus.INTERNAL_SERVER_ERROR;
|
||||||
message = '서버 내부 오류가 발생했습니다.';
|
message = '서버 내부 오류가 발생했습니다.';
|
||||||
|
|
||||||
// 개발 환경에서는 실제 에러 메시지 표시
|
if (exception instanceof Error) {
|
||||||
if (process.env.NODE_ENV === 'development' && exception instanceof Error) {
|
|
||||||
message = exception.message;
|
message = exception.message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,16 +56,37 @@ export class AllExceptionsFilter implements ExceptionFilter {
|
|||||||
message: Array.isArray(message) ? message : [message],
|
message: Array.isArray(message) ? message : [message],
|
||||||
};
|
};
|
||||||
|
|
||||||
// 개발 환경에서는 스택 트레이스 포함
|
// 스택 트레이스 포함
|
||||||
if (process.env.NODE_ENV === 'development' && exception instanceof Error) {
|
if (exception instanceof Error) {
|
||||||
(errorResponse as any).stack = exception.stack;
|
(errorResponse as any).stack = exception.stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 에러 로깅
|
// ========== 상세 로깅 ==========
|
||||||
console.error(
|
this.logger.error(
|
||||||
`[${errorResponse.timestamp}] ${request.method} ${request.url} - ${status}`,
|
`========== EXCEPTION ==========`
|
||||||
exception,
|
|
||||||
);
|
);
|
||||||
|
this.logger.error(
|
||||||
|
`[${request.method}] ${request.url} - Status: ${status}`
|
||||||
|
);
|
||||||
|
this.logger.error(
|
||||||
|
`Message: ${JSON.stringify(message)}`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (exception instanceof Error) {
|
||||||
|
this.logger.error(`Error Name: ${exception.name}`);
|
||||||
|
this.logger.error(`Error Message: ${exception.message}`);
|
||||||
|
this.logger.error(`Stack Trace:\n${exception.stack}`);
|
||||||
|
} else {
|
||||||
|
this.logger.error(`Exception: ${JSON.stringify(exception)}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Request Body 로깅 (민감정보 마스킹)
|
||||||
|
const safeBody = { ...request.body };
|
||||||
|
if (safeBody.password) safeBody.password = '****';
|
||||||
|
if (safeBody.token) safeBody.token = '****';
|
||||||
|
this.logger.error(`Request Body: ${JSON.stringify(safeBody)}`);
|
||||||
|
|
||||||
|
this.logger.error(`===============================`);
|
||||||
|
|
||||||
response.status(status).json(errorResponse);
|
response.status(status).json(errorResponse);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user