-
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.
setting: Swagger 및 Winston 모듈 세팅 (#2)
- Loading branch information
Showing
7 changed files
with
122 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,4 +45,4 @@ jobs: | |
run: pnpm lint | ||
|
||
- name: Run Prettier | ||
run: pnpm prettier | ||
run: pnpm run format |
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,10 +1,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { | ||
Logger, | ||
type MiddlewareConsumer, | ||
Module, | ||
NestModule, | ||
} from '@nestjs/common'; | ||
|
||
import { LoggerMiddleware } from '#/middlewares/logger.middleware'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
|
||
@Module({ | ||
controllers: [AppController], | ||
providers: [AppService], | ||
providers: [AppService, Logger], | ||
}) | ||
export class AppModule {} | ||
export class AppModule implements NestModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer.apply(LoggerMiddleware).forRoutes('*'); | ||
} | ||
} |
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,43 +1,23 @@ | ||
import { LoggerService } from '@nestjs/common'; | ||
|
||
import { utilities } from 'nest-winston'; | ||
import * as winston from 'winston'; | ||
|
||
const { combine, timestamp } = winston.format; | ||
|
||
export class WinstonLoggerService implements LoggerService { | ||
private logger: winston.Logger; | ||
|
||
constructor(service: string) { | ||
this.logger = winston.createLogger({ | ||
transports: [ | ||
new winston.transports.Console({ | ||
level: 'debug', | ||
format: combine( | ||
import { WinstonModule, utilities } from 'nest-winston'; | ||
import { format, transports } from 'winston'; | ||
|
||
const isProduction = process.env.NODE_ENV === 'production'; | ||
const { combine, timestamp, simple, ms } = format; | ||
|
||
export const winstonLogger = WinstonModule.createLogger({ | ||
transports: [ | ||
new transports.Console({ | ||
level: isProduction ? 'http' : 'debug', | ||
format: isProduction | ||
? simple() | ||
: combine( | ||
timestamp({ format: 'isoDateTime' }), | ||
utilities.format.nestLike(service, { | ||
prettyPrint: true, | ||
ms(), | ||
utilities.format.nestLike('dev-malssami', { | ||
colors: true, | ||
prettyPrint: true, | ||
}), | ||
), | ||
}), | ||
], | ||
}); | ||
} | ||
|
||
log(message: string) { | ||
this.logger.log({ level: 'info', message }); | ||
} | ||
|
||
error(message: string, errorStackTrace: string) { | ||
this.logger.error(message, errorStackTrace); | ||
} | ||
|
||
warn(message: string) { | ||
this.logger.warning(message); | ||
} | ||
|
||
info(message: string) { | ||
this.logger.info(message); | ||
} | ||
} | ||
}), | ||
], | ||
}); |
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
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,9 +1,20 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import type { NestExpressApplication } from '@nestjs/platform-express'; | ||
|
||
import { winstonLogger } from '#/configs/logger.config'; | ||
import { setupSwaggerModule } from '#/configs/swagger.config'; | ||
import { setupExceptionFilter } from '#/middlewares/http-exception.filter'; | ||
import { AppModule } from './app.module'; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(AppModule); | ||
const app = await NestFactory.create<NestExpressApplication>(AppModule, { | ||
bufferLogs: true, | ||
logger: winstonLogger, | ||
}); | ||
|
||
setupSwaggerModule(app); | ||
setupExceptionFilter(app); | ||
|
||
await app.listen(8080); | ||
} | ||
bootstrap(); |
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 { | ||
type ArgumentsHost, | ||
type ExceptionFilter, | ||
type HttpException, | ||
HttpStatus, | ||
INestApplication, | ||
Logger, | ||
} from '@nestjs/common'; | ||
|
||
import type { Request, Response } from 'express'; | ||
|
||
export class HttpExceptionFilter implements ExceptionFilter { | ||
catch(exception: HttpException, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse<Response>(); | ||
const request = ctx.getRequest<Request>(); | ||
const statusCode = exception.getStatus(); | ||
|
||
const errorResponse = { | ||
statusCode, | ||
timestamp: new Date().toISOString(), | ||
path: request.url, | ||
method: request.method, | ||
}; | ||
|
||
switch (statusCode) { | ||
case HttpStatus.INTERNAL_SERVER_ERROR: { | ||
Logger.error( | ||
'SERVER Error', | ||
JSON.stringify(errorResponse), | ||
exception.stack, | ||
); | ||
} | ||
default: { | ||
Logger.warn('HTTP Error', JSON.stringify(errorResponse)); | ||
} | ||
} | ||
|
||
response.status(statusCode).json(errorResponse); | ||
} | ||
} | ||
|
||
export const setupExceptionFilter = (app: INestApplication) => { | ||
app.useGlobalFilters(new HttpExceptionFilter()); | ||
}; |
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,31 @@ | ||
import { | ||
Inject, | ||
Injectable, | ||
Logger, | ||
LoggerService, | ||
NestMiddleware, | ||
} from '@nestjs/common'; | ||
|
||
import type { NextFunction, Request, Response } from 'express'; | ||
|
||
@Injectable() | ||
export class LoggerMiddleware implements NestMiddleware { | ||
constructor(@Inject(Logger) private readonly logger: LoggerService) {} | ||
|
||
use(request: Request, response: Response, next: NextFunction) { | ||
const { method, url, query, body } = request; | ||
const { statusCode } = response; | ||
|
||
this.logger.log( | ||
JSON.stringify({ | ||
statusCode, | ||
method, | ||
url, | ||
query, | ||
body, | ||
}), | ||
); | ||
|
||
next(); | ||
} | ||
} |