Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit bf665bf

Browse files
authored
chore: add request logger (#655)
1 parent bffedf5 commit bf665bf

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

cortex-js/src/app.module.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Module } from '@nestjs/common';
1+
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
22
import { MessagesModule } from './usecases/messages/messages.module';
33
import { ThreadsModule } from './usecases/threads/threads.module';
44
import { ModelsModule } from './usecases/models/models.module';
@@ -12,6 +12,7 @@ import { ConfigModule } from '@nestjs/config';
1212
import { env } from 'node:process';
1313
import { SeedService } from './usecases/seed/seed.service';
1414
import { 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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

cortex-js/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

6868
const buildSwagger = (app: INestApplication<any>) => {

0 commit comments

Comments
 (0)