In this lesson, you will learn how to architect your logging infrastructure to simultaneously output data to local human-readable streams during development and high-performance remote sinks in production.
The Problem: Local vs. Remote Needs
In local development, you need immediate, readable feedback. You want logs color-coded in your terminal so you can spot errors while iterating. In production, however, console.log is insufficient. You need structured, machine-readable JSON logs that are asynchronously transported to a log aggregation service (like ELK, Datadog, or CloudWatch) without blocking your event loop.
Implementing Dual-Transport
The strategy is to define a logging utility that initializes multiple "transports"—destinations for your log messages. We configure these transports conditionally based on the NODE_ENV environment variable.
const logger = pino({
level: process.env.LOG_LEVEL || 'info',
transport: {
targets: [
// Transport 1: Local readable output for development
{
target: 'pino-pretty',
level: 'debug',
options: { colorize: true }
},
// Transport 2: Remote stream for production
{
target: 'pino/file',
level: 'warn',
options: { destination: '/var/log/app/production.log' }
}
]
}
});
Strategic Routing
When configuring multi-transports, it is vital to set different log levels for each sink. For example, your local terminal transport can be set to debug to help you track state changes, while your remote production sink should be set to warn or error to minimize cloud ingestion costs and prevent storage bloat.
By separating these concerns, you ensure that your local "noise" never impacts the "signal" found in your production telemetry. Your application code remains clean—calling logger.info()—while the underlying configuration handles the complexity of dispatching that event to the appropriate local or remote destinations based on your runtime environment.
Key Takeaway: Multi-transport configurations allow you to decouple your application logic from observability infrastructure by routing different log levels to specific destinations based on the deployment environment.