This repository was archived by the owner on Jul 4, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +41
-3
lines changed
infrastructure/middlewares Expand file tree Collapse file tree 3 files changed +41
-3
lines changed Original file line number Diff line number Diff line change 1- import { Module } from '@nestjs/common' ;
1+ import { MiddlewareConsumer , Module , NestModule } from '@nestjs/common' ;
22import { MessagesModule } from './usecases/messages/messages.module' ;
33import { ThreadsModule } from './usecases/threads/threads.module' ;
44import { ModelsModule } from './usecases/models/models.module' ;
@@ -12,6 +12,7 @@ import { ConfigModule } from '@nestjs/config';
1212import { env } from 'node:process' ;
1313import { SeedService } from './usecases/seed/seed.service' ;
1414import { FileManagerModule } from './file-manager/file-manager.module' ;
15+ import { AppLoggerMiddleware } from './infrastructure/middlewares/app.logger.middleware' ;
1516
1617@Module ( {
1718 imports : [
@@ -34,4 +35,8 @@ import { FileManagerModule } from './file-manager/file-manager.module';
3435 ] ,
3536 providers : [ SeedService ] ,
3637} )
37- export class AppModule { }
38+ export class AppModule implements NestModule {
39+ configure ( consumer : MiddlewareConsumer ) : void {
40+ consumer . apply ( AppLoggerMiddleware ) . forRoutes ( '*' ) ;
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ import { Injectable , NestMiddleware , Logger } from '@nestjs/common' ;
2+
3+ import { Request , Response , NextFunction } from 'express' ;
4+
5+ @Injectable ( )
6+ export class AppLoggerMiddleware implements NestMiddleware {
7+ private logger = new Logger ( 'HTTP' ) ;
8+
9+ use ( req : Request , res : Response , next : NextFunction ) : void {
10+ const userAgent = req . get ( 'user-agent' ) ?? '' ;
11+ const { ip, method, path : url , originalUrl } = req ;
12+ //Setting the x-correlation-id
13+ const correlationHeader = req . header ( 'x-correlation-id' ) ?? '' ;
14+ req . headers [ 'x-correlation-id' ] = correlationHeader ;
15+ res . set ( 'X-Correlation-Id' , correlationHeader ) ;
16+ res . on ( 'close' , ( ) => {
17+ const { statusCode } = res ;
18+ const contentLength = res . get ( 'content-length' ) ;
19+ this . logger . log (
20+ JSON . stringify ( {
21+ method : method ,
22+ path : originalUrl ?? url ,
23+ statusCode : statusCode ,
24+ ip : ip ,
25+ content_length : contentLength ,
26+ user_agent : userAgent ,
27+ x_correlation_id : req . headers [ 'x-correlation-id' ] ,
28+ } ) ,
29+ ) ;
30+ } ) ;
31+ next ( ) ;
32+ }
33+ }
Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ async function bootstrap() {
6262 const port = process . env . CORTEX_JS_PORT || defaultCortexJsPort ;
6363
6464 await app . listen ( port , host ) ;
65- console . log ( `Server running on ${ host } :${ port } ` ) ;
65+ console . log ( `Server running on http:// ${ host } :${ port } ` ) ;
6666}
6767
6868const buildSwagger = ( app : INestApplication < any > ) => {
You can’t perform that action at this time.
0 commit comments