Logging Best Practices
Log actionable fields; sample debug in prod sparingly. These rules keep Node.js logs searchable, compliant, and cheap at scale.
Search across all documentation pages
Log actionable fields; sample debug in prod sparingly. These rules keep Node.js logs searchable, compliant, and cheap at scale.
requestId, tenantId) across microservices in a shared doc.event or action field for business logs. order_created, not vague done.info for request lifecycle and business events. error for failures needing action.debug off or sampled in production. LOG_LEVEL=info default; tail sampling for investigations.console.log in src/ via ESLint. Exception for CLI entrypoints only.x-request-id at edge. Return same ID in response header.requestId on every line. No manual pass-through in deep layers without ALS.x-request-id on outbound HTTP and queue jobs. End-to-end correlation./health or log at debug.userId/tenantId after auth on child logger. Not before authentication succeeds.redact for authorization, cookies, passwords, PAN. See pino.req and user objects. Log IDs, not emails.req.body or Axios error.response.data by default.pino-pretty in production. JSON only; pretty transport blocks the event loop.base: { service, version }. Searchable fleet metadata.trace_id with OpenTelemetry when enabled. Correlate logs and traces in APM.fatal pages; error tickets; warn dashboard.LOGGING.md for new services.Minimum: level, time, msg, service, requestId (HTTP), event or equivalent action key.
Full err object in logs; generic message in HTTP response. Never the reverse.
Wrap Pino for new code; migrate hot paths first. Fastify/NestJS favor Pino natively.
No in k8s - stdout only. Platform collects and indexes.
Pino is cheap relative to HTTP work. Cost explosion comes from debug verbosity and huge payloads.
Sample debug 1-5% in prod if needed. Never sample errors or payment events.
Always include tenantId on child logger after tenant resolution. Critical for support queries.
Separate stream or audit: true field with longer retention and stricter access RBAC.
Stack versions: This page was written for Node.js 24.18.0 (Active LTS), npm 10+, TypeScript 5.6+, Express 5, Fastify 5, and NestJS 11.
Reviewed by Chris St. John·Last updated Jul 16, 2026