diff --git a/BE/src/app.module.ts b/BE/src/app.module.ts index fac6872..62133d9 100644 --- a/BE/src/app.module.ts +++ b/BE/src/app.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { TypeOrmModule } from '@nestjs/typeorm'; @@ -15,6 +15,7 @@ import { AuthModule } from './auth/auth.module'; import { ServeStaticModule } from '@nestjs/serve-static'; import { join } from 'path'; import { Mates } from './mates/mates.entity'; +import { LoggingMiddleware } from './common/logging.middleware'; @Module({ imports: [ @@ -49,4 +50,8 @@ import { Mates } from './mates/mates.entity'; controllers: [AppController], providers: [AppService], }) -export class AppModule {} +export class AppModule implements NestModule { + configure(consumer: MiddlewareConsumer) { + consumer.apply(LoggingMiddleware).forRoutes('*'); + } +} diff --git a/BE/src/common/logging.interceptor.ts b/BE/src/common/logging.interceptor.ts index a137735..02cf1ac 100644 --- a/BE/src/common/logging.interceptor.ts +++ b/BE/src/common/logging.interceptor.ts @@ -17,20 +17,18 @@ export class LoggingInterceptor implements NestInterceptor { const response = ctx.getResponse(); const { method, url, body } = request; - this.logger.debug( - `[Request] Method: ${method}, URL: ${url}, Body: ${JSON.stringify(body)}`, + this.logger.debug(`[Request] ${method} ${url} \n ${JSON.stringify(body)}`); + return next.handle().pipe( + tap((body) => { + const requestToResponse: `${number}ms` = `${ + Date.now() - request.now + }ms`; + this.logger.debug( + `[Response] ${ + response.statusCode + } ${requestToResponse} \n ${JSON.stringify(body)} \n`, + ); + }), ); - - return next - .handle() - .pipe( - tap((body) => - this.logger.debug( - `[Response] Status: ${response.statusCode}, Body: ${JSON.stringify( - body, - )}`, - ), - ), - ); } } diff --git a/BE/src/common/logging.middleware.ts b/BE/src/common/logging.middleware.ts new file mode 100644 index 0000000..38537f1 --- /dev/null +++ b/BE/src/common/logging.middleware.ts @@ -0,0 +1,10 @@ +import { Injectable, NestMiddleware } from '@nestjs/common'; +import { NextFunction } from 'express'; + +@Injectable() +export class LoggingMiddleware implements NestMiddleware { + use(req: Request & { now: number }, res: Response, next: NextFunction) { + req.now = Date.now(); + next(); + } +}