Skip to content

Instantly share code, notes, and snippets.

@Vatsalya-singhi
Created March 27, 2025 18:46
Show Gist options
  • Save Vatsalya-singhi/4ba40ed998dbf67f8809f71bd6eafacf to your computer and use it in GitHub Desktop.
Save Vatsalya-singhi/4ba40ed998dbf67f8809f71bd6eafacf to your computer and use it in GitHub Desktop.
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } 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> {
console.log('Before handler execution');
const startTime = new Date();
// Proceed with the request and log the response
return next.handle().pipe(
map((data)=> ({data:data, timeToCompletion: `${new Date() - startTime} ms` })),
tap(() => console.log('After handler execution'))
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment