Pino Multi-Transport Log Routing
Route Pino logs to multiple transports in parallel, sending errors to a dedicated sink while pretty console output stays development-only.
Code
import { pino } from 'pino';
const isProd = process.env.NODE_ENV === 'production';
const logger = pino({
level: process.env.LOG_LEVEL ?? 'info',
base: { service: 'billing', region: 'eu-1' },
transports: isProd
? [
{
level: 'info',
target: 'pino/file',
options: { destination: 1, mkdir: false },
},
{
level: 'error',
target: 'pino-elasticsearch',
options: {
node: 'http://elastic:9200',
index: 'billing-errors',
flushInterval: 2000,
},
},
]
: [
{
target: 'pino-pretty',
options: { colorize: true, translateTime: 'SYS:HH:MM:ss' },
},
],
});
logger.info({ invoiceId: 'inv-9', amount: 4200 }, 'invoice settled');
logger.warn({ retries: 2 }, 'settlement retried');
logger.error({ invoiceId: 'inv-10', reason: 'gateway timeout' }, 'settlement failed');
setInterval(() => {
logger.debug('heartbeat');
}, 30_000).unref();What the code does
Everything borrowed from pino is visible in one line: pino.
Top-level bindings isProd, logger hold the demo state — nothing sits inside a function scope.
Configuration comes from the environment — it reads NODE_ENV, LOG_LEVEL, so the same code switches behavior between environments without edits.
Emits through the .info, .warn, .error, .debug level methods with structured payloads, so downstream transports can route by level and field.
Watch out for
- Plan for an install step:
pinois a real dependency, resolved by the package manager.
Statements that matter
setInterval(() => {
logger.debug('heartbeat');
}, 30_000).unref();
Interactive Tools for This Topic
HTTP Retry & Backoff CalculatorSimulate exponential backoff retry schedules with Full, Equal, Decorrelated, or No Jitter to inspect per-attempt wait times and cumulative latency.Nginx Location Block TesterTest any URL path against Nginx location blocks using authentic Nginx precedence (= exact, ^~ prefix, regex ~ and ~*, and longest prefix) with trace output.URL Pattern MatcherTest Express-style URL patterns with :param and wildcard segments against concrete paths and extract the captured parameter values.
FAQ
- What does this need from the package registry?
- These are real dependencies: `pino`. Without it the snippet will not load.
Report a problem with this exampleOpens a pre-filled issue on GitHub.