Spring Fresh Sale! - Up To 67% OFF BDIX Hosting + Free Domain
Node.js

How to Set Up Structured Logging in Node.js with Pino or Winston

Structured logging records your logs as consistent, machine-readable data (usually JSON) with levels and context — instead of unstructured console.log lines you cannot search or filter. For any real application, it is the difference between guessing and knowing when something goes wrong.

Why console.log isn't enough

Plain console.log gives you unlevelled, unstructured text with no timestamps or context by default. You cannot easily filter by severity, search by request, or feed it into a log system. Structured logging fixes all of that.

Pino vs Winston

  • Pino — extremely fast, JSON-first, minimal overhead. A great default for performance-sensitive apps.
  • Winston — flexible, with many transports (files, external services) and formats. Good when you need lots of routing options.

A basic Pino setup

const logger = require('pino')();
logger.info({ userId: 42 }, 'user logged in');
logger.error({ err }, 'payment failed');

Each line is structured JSON with a level, timestamp and your context object — searchable and filterable.

Good logging practices

  • Use levels — debug, info, warn, error — and set the threshold per environment.
  • Add context — request IDs, user IDs — so you can trace one request across many log lines.
  • Never log secrets — passwords, tokens, card numbers must never hit your logs — see managing secrets.
  • Log errors fully, including stack traces, to support error handling and monitoring.

Frequently asked questions

Pino or Winston — which should I pick?

Pino if performance and simple JSON logging matter most; Winston if you need flexible routing to multiple destinations and formats. Both are solid; many apps are happy with Pino's speed and simplicity.

Should logs be JSON in production?

Yes — JSON logs are easy for log systems to parse, search and alert on. In development you can pretty-print them for readability, then switch to raw JSON in production.

Where should logs go?

Write to standard output and let your process manager or platform collect them, or ship them to a log service. Avoid writing giant log files that fill your disk unmanaged.

Was this article helpful?