0
0
NestJSframework~5 mins

Logging with Winston or Pino in NestJS

Choose your learning style9 modes available
Introduction

Logging helps you see what your app is doing. Winston and Pino are tools that make logging easy and organized.

You want to track errors in your NestJS app.
You need to record important events for debugging.
You want to save logs to files or external services.
You want fast and structured logs for better analysis.
You want to customize log formats and levels.
Syntax
NestJS
import { LoggerModule } from 'nestjs-pino';

@Module({
  imports: [
    LoggerModule.forRoot({
      pinoHttp: {
        level: 'info',
        prettyPrint: true
      },
    }),
  ],
})
export class AppModule {}

Use LoggerModule.forRoot() to set up Pino logging in NestJS.

You can change level to control log detail (e.g., 'error', 'debug').

Examples
Example of setting up Winston with console logging at debug level.
NestJS
import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';

@Module({
  imports: [
    WinstonModule.forRoot({
      transports: [
        new winston.transports.Console({ level: 'debug' }),
      ],
    }),
  ],
})
export class AppModule {}
Using NestJS built-in Logger with custom context.
NestJS
import { Logger } from '@nestjs/common';

const logger = new Logger('MyService');
logger.log('This is an info message');
logger.error('This is an error message');
Pino setup that hides sensitive data like authorization headers.
NestJS
import { LoggerModule } from 'nestjs-pino';

@Module({
  imports: [
    LoggerModule.forRoot({
      pinoHttp: {
        level: 'error',
        redact: ['req.headers.authorization'],
      },
    }),
  ],
})
export class AppModule {}
Sample Program

This NestJS app uses Pino for logging. When you call the root URL, it logs an info message and returns 'Hello World!'.

NestJS
import { Module, Injectable } from '@nestjs/common';
import { LoggerModule } from 'nestjs-pino';
import { Controller, Get } from '@nestjs/common';
import { Logger as PinoLogger } from 'nestjs-pino';

@Injectable()
class AppService {
  constructor(private readonly logger: PinoLogger) {}

  getHello(): string {
    this.logger.info('Hello endpoint was called');
    return 'Hello World!';
  }
}

@Controller()
class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}

@Module({
  imports: [
    LoggerModule.forRoot({
      pinoHttp: {
        level: 'info',
        prettyPrint: true
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
OutputSuccess
Important Notes

Winston is very flexible and supports many transports (console, files, remote servers).

Pino is very fast and produces JSON logs by default, good for machines to read.

Always avoid logging sensitive data like passwords or tokens.

Summary

Logging helps track app behavior and errors.

Winston and Pino are popular logging tools in NestJS.

Choose Winston for flexibility, Pino for speed and structured logs.