Skip to content

Commit

Permalink
chore: adding access log
Browse files Browse the repository at this point in the history
  • Loading branch information
artursudnik committed Oct 16, 2023
1 parent 2a1de22 commit 8192cc8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
11 changes: 9 additions & 2 deletions apps/vc-api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { DynamicModule, Module } from '@nestjs/common';
import { DynamicModule, MiddlewareConsumer, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { KeyModule } from './key/key.module';
import { DidModule } from './did/did.module';
Expand All @@ -25,6 +25,7 @@ import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { SeederModule } from './seeder/seeder.module';
import { envVarsValidationSchema } from './config/env-vars-validation-schema';
import { HttpLoggerMiddleware } from './middlewares';

let config: DynamicModule;

Expand Down Expand Up @@ -61,4 +62,10 @@ try {
SeederModule
]
})
export class AppModule {}
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(HttpLoggerMiddleware) //, HttpsRedirectMiddleware) - Disabling for now, doesn't work as expected
.forRoutes('*');
}
}
47 changes: 47 additions & 0 deletions apps/vc-api/src/middlewares/http-logger.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Injectable, Logger, NestMiddleware } from '@nestjs/common';
import { NextFunction, Request, Response } from 'express';

@Injectable()
export class HttpLoggerMiddleware implements NestMiddleware {
private readonly logger = new Logger(HttpLoggerMiddleware.name, { timestamp: true });

use(req: Request, res: Response, next: NextFunction) {
const requestStarted = Date.now();
const { method } = req;
const url = req.originalUrl;

let finished = false;

res.on('finish', () => {
const message = `${res.statusCode} ${res.statusMessage} | [${method}] ${url} - ${
Date.now() - requestStarted
}ms`;

finished = true;

if (this.is4xxErrorCode(res.statusCode)) {
this.logger.warn(message);
} else if (this.is5xxErrorCode(res.statusCode)) {
this.logger.error(message);
} else {
this.logger.log(message);
}
});

res.on('close', () => {
if (!finished) {
this.logger.warn(`connection closed | [${method}] ${url} - ${Date.now() - requestStarted}ms`);
}
});

next();
}

is4xxErrorCode(statusCode: number): boolean {
return statusCode >= 400 && statusCode < 500;
}

is5xxErrorCode(statusCode: number): boolean {
return statusCode >= 500 && statusCode < 600;
}
}
1 change: 1 addition & 0 deletions apps/vc-api/src/middlewares/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './http-logger.middleware';

0 comments on commit 8192cc8

Please sign in to comment.