Node.js
backendnodejsGuideIntermediate
Node.js Logging & Telemetry
Implement structured JSON logging, log levels, correlation IDs, and high-performance loggers like Pino in Node.js.
Verified: 2026-09-30
Tested on:node 22.x
Platforms:
node
#nodejs#logging#pino#telemetry#json
Why console.log is Bad in Production
- Synchronous Bottleneck: Direct
console.logto stdout is blocking in certain operating system environments, reducing server throughput under heavy load. - Unstructured Text: Plain text strings cannot be easily queried, indexed, or filtered by centralized log collectors (Datadog, Elastic, Grafana Loki, CloudWatch).
- Missing Context: Lacks timestamp metadata, log levels, error stack traces, and request correlation IDs.
High-Performance Structured Logging with Pino
Pino is one of the fastest JSON loggers available for Node.js:
pnpm add pino
pnpm add -D pino-pretty// src/logger.js
import pino from 'pino';
export const logger = pino({
level: process.env.LOG_LEVEL || 'info',
// Pretty-print only in local development
transport: process.env.NODE_ENV === 'development' ? {
target: 'pino-pretty',
options: { colorize: true },
} : undefined,
});Standard Log Levels
| Level | When to Use |
|---|---|
fatal | Catastrophic failure causing application exit (e.g. database unconnectable). |
error | Operation failed (e.g. payment gateway timeout, uncaught route rejection). |
warn | Abnormal condition that didn't stop execution (e.g. deprecated API call). |
info | Normal operational events (e.g. server booted on port 3000, batch job started). |
debug | Diagnostic data for local troubleshooting (e.g. resolved config values). |
trace | Extremely granular execution details (e.g. SQL query payloads). |
Adding Request Correlation IDs
Attaching a unique correlationId or reqId to every log line allows you to trace a single user request across distributed microservices:
// Middleware example
import crypto from 'node:crypto';
import { logger } from './logger.js';
export function requestLogger(req, res, next) {
const reqId = req.headers['x-request-id'] || crypto.randomUUID();
const childLogger = logger.child({ reqId, path: req.url, method: req.method });
req.log = childLogger;
childLogger.info('Incoming request');
res.on('finish', () => {
childLogger.info({ statusCode: res.statusCode }, 'Request completed');
});
next();
}Node.js Process Management & Graceful Shutdown
Manage process lifecycles, handle SIGTERM/SIGINT OS signals, monitor memory, and implement graceful shutdown in production.
Node.js Security Fundamentals
Defend Node.js applications against prototype pollution, ReDoS, SQL injection, cross-site scripting, and dependency vulnerabilities.