@bluealba/nestjs-pino
v1.0.0
Published
NestJS pino based logger module including pino-http
Readme
@bluealba/nestjs-pino
A NestJS logger module built on top of nestjs-pino with opinionated defaults for production-ready logging using Pino and pino-http.
Features
- High-performance JSON logging with Pino
- HTTP request/response logging with pino-http
- Automatic logger context based on the injecting class
- Buffered asynchronous logging for optimal performance
- Automatic redaction of sensitive headers
- Global module - import once, use everywhere
Installation
npm install @bluealba/nestjs-pinoUsage
1. Import the LoggerModule
Import LoggerModule in your application's root module:
import { Module } from '@nestjs/common';
import { LoggerModule } from '@bluealba/nestjs-pino';
@Module({
imports: [LoggerModule],
// ...
})
export class AppModule {}Since LoggerModule is a global module, you only need to import it once in your root module. It will be available throughout your entire application.
2. Inject the Logger
Important: The logger must be injected via dependency injection. Inject the Logger into any service, controller, or provider:
import { Injectable } from '@nestjs/common';
import { Logger } from '@bluealba/nestjs-pino';
@Injectable()
export class UserService {
constructor(private readonly logger: Logger) {}
async findUser(id: string) {
this.logger.info({ userId: id }, 'Finding user');
// ...
}
}3. Automatic Context Setting
The logger context is automatically set based on the class where it's injected. You do not need to call setContext() manually.
@Injectable()
export class UserService {
constructor(private readonly logger: Logger) {
// Context is automatically set to "UserService"
// No need to call: this.logger.setContext('UserService')
}
createUser(data: CreateUserDto) {
// Logs will include: "context": "UserService"
this.logger.info({ data }, 'Creating user');
}
}The context is derived from the class constructor name, making it easy to identify which part of your application generated each log entry.
4. Logging Methods
The logger provides all standard Pino logging methods:
this.logger.trace('Trace message');
this.logger.debug({ data }, 'Debug message');
this.logger.info({ userId: '123' }, 'Info message');
this.logger.warn('Warning message');
this.logger.error({ err }, 'Error message');
this.logger.fatal('Fatal message');The log() method is also available and maps to info():
this.logger.log('This is an info message');Default Configuration
This library comes with production-ready defaults:
Logging Performance
- Asynchronous logging: Logs are written asynchronously to avoid blocking the event loop
- Buffering:
- Minimum buffer length: 512 bytes
- Maximum buffer length: 128 KB
- Maximum write size: 128 KB
- Periodic flush: Every 1000ms (1 second)
Security
Sensitive HTTP headers are automatically redacted from logs:
req.headers.authorizationreq.headers.cookie
Request Serialization
HTTP requests are logged with the following fields:
id: Request IDmethod: HTTP methodurl: Request URLheaders: Request headers (with sensitive data redacted)remoteAddress: Client IP addressremotePort: Client port or X-Forwarded-Port header
Timestamp Format
Logs include a Unix timestamp (milliseconds since epoch):
{
"level": 30,
"timestamp": 1705761234567,
"context": "UserService",
"msg": "User created"
}Why Use This Library?
This library wraps nestjs-pino with sensible defaults so you don't have to configure:
- Buffering and async logging for performance
- Security best practices (header redaction)
- Structured request logging
- Context management
It allows you to focus on writing logs instead of configuring the logger.
License
PolyForm-Noncommercial-1.0.0
