diff --git a/src/app.module.ts b/src/app.module.ts index abd82a0..607be0a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -14,7 +14,7 @@ import { LoggingMiddleware } from './common/logger/logger.middleware'; imports: [ LoggerModule, AuthModule, - ConfigModule.forRoot({ isGlobal: true, envFilePath: '.env' }), + ConfigModule.forRoot({ isGlobal: true }), FriendModule, ], controllers: [UserController, AppController], diff --git a/src/auth/auth.controller.spec.ts b/src/auth/auth.controller.spec.ts index fd088e5..107e340 100644 --- a/src/auth/auth.controller.spec.ts +++ b/src/auth/auth.controller.spec.ts @@ -1,9 +1,9 @@ -import { ConfigModule } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; import { AuthController } from './auth.controller'; import { AuthService } from './auth.service'; import { PrismaService } from '../prisma.service'; import { UserService } from '../user/user.service'; +import { ConfigModule } from '@nestjs/config'; import { LoggerModule } from '../common/logger/logger.module'; describe('AuthController', () => { diff --git a/src/auth/auth.module.ts b/src/auth/auth.module.ts index b050644..f18ef40 100644 --- a/src/auth/auth.module.ts +++ b/src/auth/auth.module.ts @@ -5,10 +5,8 @@ import { AuthController } from './auth.controller'; import { AuthService } from './auth.service'; import { UserService } from '../user/user.service'; import { PrismaService } from '../prisma.service'; -import { LoggerModule } from 'src/common/logger/logger.module'; @Module({ - imports: [LoggerModule], controllers: [AuthController, PasswordController], providers: [AuthService, UserService, PrismaService], }) diff --git a/src/auth/auth.service.spec.ts b/src/auth/auth.service.spec.ts index a7abcba..9b8b661 100644 --- a/src/auth/auth.service.spec.ts +++ b/src/auth/auth.service.spec.ts @@ -1,8 +1,8 @@ -import { ConfigModule } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; import { AuthService } from './auth.service'; import { PrismaService } from '../prisma.service'; import { UserService } from '../user/user.service'; +import { ConfigModule } from '@nestjs/config'; import { LoggerModule } from '../common/logger/logger.module'; describe('AuthService', () => { diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index 9d0747e..4d28bba 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -9,14 +9,14 @@ import { EmailDto } from './../dto/email.dto'; import { ForgotPasswordDto } from './../dto/forgotPassword.dto'; import { ChangePasswordDto } from './../dto/changePassword.dto'; import { Logger } from 'winston'; -import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; +import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston'; @Injectable() export class AuthService { private readonly cognitoClient: CognitoIdentityServiceProvider; private readonly clientId: string; constructor( - @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger, + @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger: Logger, private readonly configService: ConfigService, private readonly userService: UserService, ) { diff --git a/src/common/logger/logger.config.ts b/src/common/logger/logger.config.ts index f84d210..c366200 100644 --- a/src/common/logger/logger.config.ts +++ b/src/common/logger/logger.config.ts @@ -12,7 +12,7 @@ export const appendTimestamp = winston.format((info, opts) => { // 로그 저장 파일 옵션 export const dailyOptions = (level: string) => { return { - level: 'info', + level: level, datePattern: 'YYYY-MM-DD', // 날짜 포맷 dirname: `${appRootPath.path}/logs` + `/${level}`, // 저장할 URL: 여기서는 루트에 logs라는 폴더가 생기고 그 아래에 level 폴더 filename: `%DATE%.${level}.log`, diff --git a/src/common/logger/logger.middleware.ts b/src/common/logger/logger.middleware.ts index 3731419..c6d8ef1 100644 --- a/src/common/logger/logger.middleware.ts +++ b/src/common/logger/logger.middleware.ts @@ -8,8 +8,8 @@ export class LoggingMiddleware implements NestMiddleware { use(req: Request, res: Response, next: NextFunction) { const { ip, method, originalUrl } = req; const userAgent = req.get('user-agent') || ''; - const reqHeaders = JSON.stringify(req.headers); - const reqBody = JSON.stringify(req.body); + const reqHeaders = JSON.stringify(req.headers, null, 2); + const reqBody = JSON.stringify(req.body, null, 2); const chunks: Buffer[] = []; @@ -25,13 +25,24 @@ export class LoggingMiddleware implements NestMiddleware { chunks.push(Buffer.from(args[0])); } const responseBody = Buffer.concat(chunks).toString('utf-8'); - const resHeaders = JSON.stringify(res.getHeaders()); + const resHeaders = JSON.stringify(res.getHeaders(), null, 2); const { statusCode, statusMessage } = res; const logLevel = statusCode >= 400 ? 'error' : 'log'; + const levelColor = + statusCode >= 400 + ? '🟥🟥🟥🟥🟥🟥🟥🟥🟥🟥' + : statusCode >= 300 + ? '🟧🟧🟧🟧🟧🟧🟧🟧🟧🟧' + : '🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩'; this.logger[logLevel]( - `Request from ${ip} to ${method} ${originalUrl} - ${statusCode} ${statusMessage} - ${userAgent} \nReq Headers: ${reqHeaders} \nReq Body: ${reqBody} \nRes Headers: ${resHeaders} \nRes Body: ${responseBody}`, + `Request from ${ip} to ${method} ${originalUrl} - ${statusCode} ${statusMessage} - ${userAgent}\n\n` + + `${levelColor}\n` + + `👩 Req Headers:\n${reqHeaders}\n\n` + + `👚 Req Body:\n${reqBody}\n\n` + + `👨 Res Headers:\n${resHeaders}\n\n` + + `👕 Res Body:\n${responseBody}\n`, ); return originalEnd(...args); diff --git a/src/common/logger/logger.module.ts b/src/common/logger/logger.module.ts index 0b3cfd1..80301fc 100644 --- a/src/common/logger/logger.module.ts +++ b/src/common/logger/logger.module.ts @@ -25,7 +25,7 @@ import { dailyOptions } from './logger.config'; ), }), - new winstonDaily(dailyOptions('warn')), + new winstonDaily(dailyOptions('info')), new winstonDaily(dailyOptions('error')), new winstonDaily(dailyOptions('fatal')), ], diff --git a/src/main.ts b/src/main.ts index 0d80ce8..c8aec20 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,9 +2,12 @@ import { NestFactory } from '@nestjs/core'; import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; import { AppModule } from './app.module'; import { ValidationPipe } from '@nestjs/common'; +import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston'; async function bootstrap() { - const app = await NestFactory.create(AppModule); + const app = await NestFactory.create(AppModule, { bufferLogs: true }); + const logger = app.get(WINSTON_MODULE_NEST_PROVIDER); + app.useLogger(logger); app.useGlobalPipes( new ValidationPipe({ @@ -27,5 +30,6 @@ async function bootstrap() { }); await app.listen(80); + logger.log('Application is listening on port 80'); } bootstrap();