Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Implementing Advanced Logging in NestJS with Pino

Tech May 16 1

Pino Logging Integration

Pino is a high-performance logging library that can be seamlessly integrated into NestJS applications. For implementation details, visit:

To begin, install the NestJS Pino adapter:

npm install nestjs-pino

Module Configuration

Register the LoggerModule in your application's root module:

import { LoggerModule } from 'nestjs-pino';

@Module({
  controllers: [AppController],
  imports: [LoggerModule.forRoot()]
})
export class AppModule {}

Controller Implementation

Inject the Logger service into your controllers:

import { Controller, Logger } from 'nestjs-pino';

@Controller('user')
export class UserController {
  private readonly applicationLogger: Logger;

  constructor() {
    // The logger automatically handles request/response cycles
    // No need for manual initialization calls
  }
}

Note that Pino operates automatically without requiring manual log calls for basic functionality. However, the default output format may not be optimal for readability in development environments.

Enhancing Pino Output Readability

To improve log readability, install additional formatting and rolling file plugins:

npm install pino-pretty pino-roll

Environment-Specific Configuration

Configure different logging behaviors for development and production environments:

import { LoggerModule } from 'nestjs-pino';
import { join } from 'path';

const isDevelopment = process.env.NODE_ENV === 'development';

@Module({
  imports: [
    LoggerModule.forRoot({
      pinoHttp: {
        transport: isDevelopment ? {
          target: 'pino-pretty',
          level: 'info',
          options: {
            colorize: true,
            translateTime: 'SYS:standard',
            ignore: 'pid,hostname'
          }
        } : {
          target: 'pino-roll',
          level: 'info',
          options: {
            file: join('logs', 'application.log'),
            frequency: 'daily',
            size: '10m',
            mkdir: true
          }
        }
      }
    })
  ]
})
export class AppModule {}

Advanced Multi-Target Configuration

Pino supports outputting to multiple targets simultaneously. This configuration allows logs to be displayed in the console during development and written to files in production:

LoggerModule.forRoot({
  pinoHttp: {
    transport: {
      targets: [
        {
          level: 'info',
          target: 'pino-pretty',
          options: {
            colorize: true,
            translateTime: 'SYS:standard',
            ignore: 'pid,hostname'
          }
        },
        {
          target: 'pino-roll',
          level: 'info',
          options: {
            file: join('logs', 'application.log'),
            frequency: 'daily',
            size: '10m',
            mkdir: true
          }
        }
      ]
    }
  }
})

Conditional targets can also be implemented based on environment variables:

LoggerModule.forRoot({
  pinoHttp: {
    transport: {
      targets: [
        isDevelopment ? {
          level: 'debug',
          target: 'pino-pretty',
          options: {
            colorize: true,
            translateTime: 'SYS:standard',
            ignore: 'pid,hostname'
          }
        } : {
          target: 'pino-roll',
          level: 'info',
          options: {
            file: join('logs', 'application.log'),
            frequency: 'daily',
            size: '10m',
            mkdir: true
          }
        }
      ]
    }
  }
})

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.