Skip to content

Commit

Permalink
Add tracker logging interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
opcatdev committed Nov 8, 2024
1 parent 8fb93ee commit cffb1b0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/tracker/src/logging.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const request = context.switchToHttp().getRequest();
const method = request.method;
const url = request.url;
const requestId = `${Date.now()}${Math.floor(Math.random() * 1000)}`;
const startTime = Date.now();

console.log(`[${requestId}] ${method} ${url}`);
return next.handle().pipe(
tap(() => {
const responseTime = Date.now() - startTime;
console.log(`[${requestId}] Completed in ${responseTime}ms`);
}),
);
}
}
3 changes: 3 additions & 0 deletions packages/tracker/src/main-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AppApiModule } from './app-api.module';
import * as ecc from '@bitcoin-js/tiny-secp256k1-asmjs';
import { initEccLib } from 'bitcoinjs-lib';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { LoggingInterceptor } from './logging.interceptor';

async function bootstrap() {
initEccLib(ecc);
Expand Down Expand Up @@ -30,6 +31,8 @@ async function bootstrap() {
app.setGlobalPrefix('api');
app.enableCors();

app.useGlobalInterceptors(new LoggingInterceptor());

await app.listen(process.env.API_PORT || 3000);
console.log(`tracker api is running on: ${await app.getUrl()}`);
}
Expand Down

0 comments on commit cffb1b0

Please sign in to comment.