typescript 39 lines · 5 steps

How a NestJS logging interceptor works

A NestJS interceptor wraps every request to log its method, URL, and timing on both success and failure.

Explained by highlit
1import {
2 Injectable,
3 NestInterceptor,
4 ExecutionContext,
5 CallHandler,
6 Logger,
7} from '@nestjs/common';
8import { Observable } from 'rxjs';
9import { tap, catchError } from 'rxjs/operators';
10import { throwError } from 'rxjs';
11import { Request } from 'express';
12 
13@Injectable()
14export class LoggingInterceptor implements NestInterceptor {
15 private readonly logger = new Logger(LoggingInterceptor.name);
16 
17 intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
18 const request = context.switchToHttp().getRequest<Request>();
19 const { method, url } = request;
20 const handler = context.getHandler().name;
21 const now = Date.now();
22 
23 this.logger.log(`--> ${method} ${url} (${handler})`);
24 
25 return next.handle().pipe(
26 tap(() => {
27 const elapsed = Date.now() - now;
28 this.logger.log(`<-- ${method} ${url} ${elapsed}ms`);
29 }),
30 catchError((error) => {
31 const elapsed = Date.now() - now;
32 this.logger.error(
33 `<-- ${method} ${url} ${elapsed}ms FAILED: ${error.message}`,
34 );
35 return throwError(() => error);
36 }),
37 );
38 }
39}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Interceptors sit around handler execution, letting you act before and after the response without touching the route logic.
  2. 2RxJS operators like tap and catchError let you observe the response stream for both success and error paths.
  3. 3Capturing a timestamp before next.handle() lets you measure elapsed time once the observable settles.

Related explainers

typescript
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
import { ApiProperty } from '@nestjs/swagger';

Wiring validation and Swagger docs in NestJS

validation openapi decorators
Intermediate 8 steps
javascript
const ROLE_PERMISSIONS = {
  admin: ['users:read', 'users:write', 'billing:read', 'billing:write'],
  manager: ['users:read', 'billing:read'],
  member: ['users:read'],

Role-based permissions middleware in Express

authorization middleware rbac
Intermediate 9 steps
typescript
import { Component, Input } from '@angular/core';
 
interface Order {
  id: string;

How Angular ICU plurals localize an order summary

i18n pluralization standalone-component
Intermediate 8 steps
typescript
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, catchError, concatMap, finalize } from 'rxjs';
import { DataSource, QueryRunner } from 'typeorm';
 

Wrapping requests in a transaction with NestJS

interceptors transactions rxjs
Advanced 7 steps
typescript
type CsvColumn<T> = {
  header: string;
  value: (row: T) => string | number | boolean | null | undefined;
};

Building a type-safe CSV writer in TypeScript

generics serialization escaping
Intermediate 7 steps
javascript
const express = require('express');
const app = express();
 
app.get('/health', (req, res) => res.json({ status: 'ok' }));

Graceful shutdown in an Express server

graceful-shutdown signal-handling connection-tracking
Advanced 9 steps

Share this explainer

Here's the card — post it anywhere.

How a NestJS logging interceptor works — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code