Skip to main content

@nest-toolbox/http-logger-middleware

npm version

A NestJS middleware that logs incoming HTTP request details — method, URL, query parameters, route params, and body — using the built-in NestJS Logger with colorized (magenta) output.

Installation

npm install @nest-toolbox/http-logger-middleware

Quick Start

import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { HttpLoggerMiddleware } from '@nest-toolbox/http-logger-middleware';

@Module({})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(HttpLoggerMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL });
}
}

Every incoming request will now produce a log block like:

[HTTP] ---------------------------------------------------------------------------------
[HTTP] request method: POST
[HTTP] request url: http://localhost:3000/api/v1/users?page=1
[HTTP] request query: {"page":"1"}
[HTTP] request params: {"id":"42"}
[HTTP] request body: {"name":"John","email":"john@example.com"}
[HTTP] ---------------------------------------------------------------------------------

Features

  • Zero configuration — just apply the middleware, no options needed
  • Smart logging — only logs query, params, and body when they are non-empty
  • Colorized output — uses magenta-colored text via chalk for visual distinction
  • NestJS Logger integration — logs under the HTTP context, works with any custom LoggerService you've configured
  • Injectable — decorated with @Injectable(), compatible with NestJS dependency injection

API Reference

HttpLoggerMiddleware

import { HttpLoggerMiddleware } from '@nest-toolbox/http-logger-middleware';

An @Injectable() NestJS middleware class that implements NestMiddleware.

Method

use(req: Request, res: Response, next: NextFunction): void

Logs the following from each incoming Express Request:

FieldLogged WhenExample Output
methodAlwaysrequest method: GET
urlAlwaysrequest url: http://localhost:3000/api/users
queryNon-empty query stringrequest query: {"page":"1","limit":"10"}
paramsNon-empty route paramsrequest params: {"id":"42"}
bodyNon-empty request bodyrequest body: {"name":"John"}

Each log block is wrapped in separator lines (---) for readability.

Examples

Log All Routes

import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { HttpLoggerMiddleware } from '@nest-toolbox/http-logger-middleware';

@Module({
imports: [UsersModule, OrdersModule],
})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(HttpLoggerMiddleware)
.forRoutes({ path: '*', method: RequestMethod.ALL });
}
}

Log Specific Routes Only

import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { HttpLoggerMiddleware } from '@nest-toolbox/http-logger-middleware';
import { UsersController } from './users/users.controller';

@Module({
imports: [UsersModule],
})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(HttpLoggerMiddleware)
.forRoutes(
{ path: 'api/users', method: RequestMethod.ALL },
{ path: 'api/orders', method: RequestMethod.POST },
);
}
}

Combine with a Controller

import { Module, MiddlewareConsumer } from '@nestjs/common';
import { HttpLoggerMiddleware } from '@nest-toolbox/http-logger-middleware';
import { UsersController } from './users/users.controller';

@Module({})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(HttpLoggerMiddleware)
.forRoutes(UsersController);
}
}

Pair with BunyanLoggerService or WinstonLoggerService

Since HttpLoggerMiddleware uses the NestJS built-in Logger, it automatically delegates to whichever LoggerService you've configured at bootstrap:

import { NestFactory } from '@nestjs/core';
import { BunyanLoggerService } from '@nest-toolbox/bunyan-logger';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: new BunyanLoggerService({
projectId: 'my-app',
formatterOptions: { outputMode: 'short', color: true },
}),
});

await app.listen(3000);
// HttpLoggerMiddleware output will now flow through Bunyan
}
bootstrap();

Exclude Routes

import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { HttpLoggerMiddleware } from '@nest-toolbox/http-logger-middleware';

@Module({})
export class AppModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(HttpLoggerMiddleware)
.exclude(
{ path: 'health', method: RequestMethod.GET },
{ path: 'metrics', method: RequestMethod.GET },
)
.forRoutes({ path: '*', method: RequestMethod.ALL });
}
}