0
0
NestJSframework~5 mins

Logging interceptor in NestJS

Choose your learning style9 modes available
Introduction

A logging interceptor helps you see what happens inside your app by recording requests and responses. It makes debugging and monitoring easier.

You want to track every request coming to your server.
You need to log how long each request takes to process.
You want to record errors or important events automatically.
You want to keep a history of API calls for auditing.
You want to debug issues by seeing input and output data.
Syntax
NestJS
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> {
    const request = context.switchToHttp().getRequest();
    const method = request.method;
    const url = request.url;
    const now = Date.now();

    return next
      .handle()
      .pipe(
        tap(() => console.log(`${method} ${url} took ${Date.now() - now}ms`)),
      );
  }
}

The intercept method runs before and after the request handler.

Use context.switchToHttp().getRequest() to access request details.

Examples
This example logs the HTTP method and URL when a request arrives.
NestJS
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> {
    const req = context.switchToHttp().getRequest();
    console.log(`Incoming request: ${req.method} ${req.url}`);
    return next.handle();
  }
}
This example logs how long the request took to process after it finishes.
NestJS
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> {
    const now = Date.now();
    return next.handle().pipe(
      tap(() => console.log(`Request processed in ${Date.now() - now}ms`)),
    );
  }
}
This example combines logging the request method, URL, and processing time.
NestJS
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> {
    const req = context.switchToHttp().getRequest();
    const method = req.method;
    const url = req.url;
    const now = Date.now();

    return next.handle().pipe(
      tap(() => console.log(`${method} ${url} took ${Date.now() - now}ms`)),
    );
  }
}
Sample Program

This is a complete logging interceptor that logs the HTTP method, URL, and how long the request took to process.

NestJS
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> {
    const req = context.switchToHttp().getRequest();
    const method = req.method;
    const url = req.url;
    const now = Date.now();

    return next.handle().pipe(
      tap(() => console.log(`${method} ${url} took ${Date.now() - now}ms`)),
    );
  }
}
OutputSuccess
Important Notes

Make sure to register the interceptor globally or in the controller to activate it.

Logging too much data can slow down your app, so log only what you need.

You can customize the interceptor to log request body, headers, or response data if needed.

Summary

Logging interceptors help track requests and responses easily.

They run code before and after your request handlers.

Use them to measure request time and log useful info for debugging.