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.

TypescriptNodeResilience

Code

typescriptsnippet
39 lines
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: pino is a real dependency, resolved by the package manager.

Statements that matter

setInterval(() => {
  logger.debug('heartbeat');
}, 30_000).unref();

Interactive Tools for This Topic

FAQ

What does this need from the package registry?
These are real dependencies: `pino`. Without it the snippet will not load.