From 314a2e4903f188d6a940f3939a2c32b942e1f307 Mon Sep 17 00:00:00 2001 From: YeongWoooo Date: Sun, 19 May 2024 12:10:51 +0900 Subject: [PATCH] =?UTF-8?q?[CHORE]=20nestjs=20logger=20=EA=B0=9C=EC=84=A0?= =?UTF-8?q?=20#26?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/app.module.ts | 9 ++++-- .../common/middleware/logger.middleware.ts | 31 +++++++++++++++++++ server/src/main.ts | 7 ++++- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 server/src/common/middleware/logger.middleware.ts diff --git a/server/src/app.module.ts b/server/src/app.module.ts index 1b554466..41967107 100644 --- a/server/src/app.module.ts +++ b/server/src/app.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { MiddlewareConsumer, Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { MeetingModule } from './meeting/meeting.module'; import { ConfigModule, ConfigService } from '@nestjs/config'; @@ -10,6 +10,7 @@ import { TransformInterceptor } from './common/interceptor/transform.interceptor import { NoticeModule } from './notice/notice.module'; import { PostModule } from './post/post.module'; import { CommentModule } from './comment/comment.module'; +import { LoggerMiddleware } from './common/middleware/logger.middleware'; @Module({ imports: [ @@ -46,4 +47,8 @@ import { CommentModule } from './comment/comment.module'; }, ], }) -export class AppModule {} +export class AppModule { + configure(consumer: MiddlewareConsumer) { + consumer.apply(LoggerMiddleware).forRoutes('*'); + } +} diff --git a/server/src/common/middleware/logger.middleware.ts b/server/src/common/middleware/logger.middleware.ts new file mode 100644 index 00000000..eed7ac9e --- /dev/null +++ b/server/src/common/middleware/logger.middleware.ts @@ -0,0 +1,31 @@ +import { Injectable, Logger, NestMiddleware } from '@nestjs/common'; +import { NextFunction, Request, Response } from 'express'; + +@Injectable() +export class LoggerMiddleware implements NestMiddleware { + private readonly logger = new Logger('HTTP'); + + use(request: Request, response: Response, next: NextFunction) { + const start = Date.now(); + + response.on('finish', () => { + const { method, originalUrl } = request; + const { statusCode, statusMessage } = response; + const duration = Date.now() - start; + + const message = `[${duration}ms] ${method} ${originalUrl} ${statusCode} ${statusMessage}`; + + if (statusCode >= 500) { + return this.logger.error(message); + } + + if (statusCode >= 400) { + return this.logger.warn(message); + } + + return this.logger.log(message); + }); + + next(); + } +} diff --git a/server/src/main.ts b/server/src/main.ts index 8a8f54b5..b4d3a4c0 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -6,8 +6,12 @@ import { setupSwagger } from './common/utils/swagger'; async function bootstrap() { const app = await NestFactory.create(AppModule, { cors: true, - bufferLogs: true, + logger: + process.env.NODE_ENV === 'production' + ? ['log', 'error', 'warn'] + : ['log', 'error', 'warn', 'debug', 'verbose'], }); + setupSwagger(app); app.useGlobalPipes( new ValidationPipe({ @@ -23,4 +27,5 @@ async function bootstrap() { const port = process.env.PORT || 3000; await app.listen(port); } + bootstrap();