Skip to content

Commit

Permalink
chore: add request logger
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Jun 3, 2024
1 parent b879f66 commit cdaadec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
9 changes: 7 additions & 2 deletions cortex-js/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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: [
Expand All @@ -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('*');
}
}
33 changes: 33 additions & 0 deletions cortex-js/src/infrastructure/middlewares/app.logger.middleware.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
2 changes: 1 addition & 1 deletion cortex-js/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>) => {
Expand Down

0 comments on commit cdaadec

Please sign in to comment.