Pino
Ultra-fast JSON logging library for Node.js. Optimized for async operations, achieving 5-10x faster performance than Winston. Minimizes CPU usage and specializes in high-volume log processing in production environments. Supports log levels, sensitive data redaction, and stream multiplexing.
Library
Pino
Overview
Pino is a very fast JSON logging library for Node.js. It achieves excellent performance through ultra-low overhead and supports complete JSON output as structured logs. It's optimized for high-load applications in production environments.
Details
Pino is designed with the concept of "low latency logging", minimizing the performance impact of log output on applications. By leveraging Worker Threads for asynchronous processing and minimal serialization, it achieves over 6 times faster processing than traditional loggers.
Key Features
- Ultra-fast log processing: Asynchronous processing using Worker Threads
- JSON structured logs: Complete JSON format output
- Lightweight design: Minimal dependencies
- Rich transports: Support for files, streams, and external services
- Child loggers: Creation of contextual logs
Pros and Cons
Pros
- Highest performance level in Node.js ecosystem
- High compatibility with monitoring tools due to JSON format
- Easy configuration with low learning curve
- Very low memory usage
- TypeScript support ensures type safety
Cons
- JSON-only format makes it hard for humans to read
- Complex creation of custom output formats
- Limited similar libraries in other languages
- Rich features (color output, etc.) require separate plugins
Reference Pages
Code Examples
Basic Setup
npm install pino
Basic Usage
const pino = require('pino')
const logger = pino()
logger.info('Hello Pino!')
logger.error('An error occurred')
// Output example: {"level":30,"time":1640995200000,"pid":12345,"hostname":"myserver","msg":"Hello Pino!"}
Configuration and Customization
const pino = require('pino')
const logger = pino({
level: 'debug',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
translateTime: 'SYS:yyyy-mm-dd HH:MM:ss',
ignore: 'pid,hostname'
}
}
})
logger.debug('Debug message')
logger.info('Info message')
logger.warn('Warning message')
logger.error('Error message')
Structured Logging
const logger = pino()
// Log with objects
logger.info({ userId: 123, action: 'login' }, 'User logged in')
// Error object logging
try {
throw new Error('Database connection error')
} catch (error) {
logger.error({ error }, 'Database error occurred')
}
// Adding custom information
logger.info({
requestId: 'req-001',
method: 'POST',
url: '/api/users',
responseTime: 150
}, 'API request completed')
Child Loggers and Context
const logger = pino()
// Create child logger (with common properties)
const childLogger = logger.child({
module: 'user-service',
version: '1.2.0'
})
childLogger.info('User service started')
// Request-specific child logger
const requestLogger = logger.child({
requestId: 'req-123',
userId: 456
})
requestLogger.info('Processing started')
requestLogger.info('Database search completed')
requestLogger.info('Processing completed')
File Output and Rotation
const pino = require('pino')
const fs = require('fs')
// File output
const logFile = fs.createWriteStream('./app.log', { flags: 'a' })
const logger = pino(logFile)
// File output with rotation
const logger2 = pino(pino.destination({
dest: './app.log',
minLength: 4096,
sync: false
}))
// Multiple outputs
const streams = [
{ stream: process.stdout },
{ level: 'error', stream: fs.createWriteStream('./error.log') },
{ level: 'fatal', stream: process.stderr }
]
const logger3 = pino({
level: 'debug'
}, pino.multistream(streams))
Production Environment Configuration
const pino = require('pino')
const isProduction = process.env.NODE_ENV === 'production'
const logger = pino({
level: isProduction ? 'info' : 'debug',
redact: ['password', 'creditCard'], // Remove sensitive information
timestamp: pino.stdTimeFunctions.isoTime,
formatters: {
level(label) {
return { level: label }
}
},
transport: !isProduction ? {
target: 'pino-pretty',
options: { colorize: true }
} : undefined
})
module.exports = logger