From cdaadec42389dce62324ad61c5ea82cdce5ae5ce Mon Sep 17 00:00:00 2001 From: Louis Date: Mon, 3 Jun 2024 17:06:38 +0700 Subject: [PATCH] chore: add request logger --- cortex-js/src/app.module.ts | 9 +++-- .../middlewares/app.logger.middleware.ts | 33 +++++++++++++++++++ cortex-js/src/main.ts | 2 +- 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 cortex-js/src/infrastructure/middlewares/app.logger.middleware.ts diff --git a/cortex-js/src/app.module.ts b/cortex-js/src/app.module.ts index b27888ea2..e97bec2ab 100644 --- a/cortex-js/src/app.module.ts +++ b/cortex-js/src/app.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { MessagesModule } from './usecases/messages/messages.module'; import { ThreadsModule } from './usecases/threads/threads.module'; import { ModelsModule } from './usecases/models/models.module'; @@ -12,6 +12,7 @@ import { ConfigModule } from '@nestjs/config'; import { env } from 'node:process'; import { SeedService } from './usecases/seed/seed.service'; import { FileManagerModule } from './file-manager/file-manager.module'; +import { AppLoggerMiddleware } from './infrastructure/middlewares/app.logger.middleware'; @Module({ imports: [ @@ -34,4 +35,8 @@ import { FileManagerModule } from './file-manager/file-manager.module'; ], providers: [SeedService], }) -export class AppModule {} +export class AppModule implements NestModule { + configure(consumer: MiddlewareConsumer): void { + consumer.apply(AppLoggerMiddleware).forRoutes('*'); + } +} diff --git a/cortex-js/src/infrastructure/middlewares/app.logger.middleware.ts b/cortex-js/src/infrastructure/middlewares/app.logger.middleware.ts new file mode 100644 index 000000000..02880f110 --- /dev/null +++ b/cortex-js/src/infrastructure/middlewares/app.logger.middleware.ts @@ -0,0 +1,33 @@ +import { Injectable, NestMiddleware, Logger } from '@nestjs/common'; + +import { Request, Response, NextFunction } from 'express'; + +@Injectable() +export class AppLoggerMiddleware implements NestMiddleware { + private logger = new Logger('HTTP'); + + use(req: Request, res: Response, next: NextFunction): void { + const userAgent = req.get('user-agent') ?? ''; + const { ip, method, path: url, originalUrl } = req; + //Setting the x-correlation-id + const correlationHeader = req.header('x-correlation-id') ?? ''; + req.headers['x-correlation-id'] = correlationHeader; + res.set('X-Correlation-Id', correlationHeader); + res.on('close', () => { + const { statusCode } = res; + const contentLength = res.get('content-length'); + this.logger.log( + JSON.stringify({ + method: method, + path: originalUrl ?? url, + statusCode: statusCode, + ip: ip, + content_length: contentLength, + user_agent: userAgent, + x_correlation_id: req.headers['x-correlation-id'], + }), + ); + }); + next(); + } +} diff --git a/cortex-js/src/main.ts b/cortex-js/src/main.ts index 3bcab01b7..f0e2b6cda 100644 --- a/cortex-js/src/main.ts +++ b/cortex-js/src/main.ts @@ -62,7 +62,7 @@ async function bootstrap() { const port = process.env.CORTEX_JS_PORT || defaultCortexJsPort; await app.listen(port, host); - console.log(`Server running on ${host}:${port}`); + console.log(`Server running on http://${host}:${port}`); } const buildSwagger = (app: INestApplication) => {