Logging (Node.js)
Logging in Node.js records what your app does for debugging and monitoring. console works for development, while structured loggers like Winston and Pino add log levels, timestamps, and JSON output for production.
Node.js
Logs are how you understand a running app. console.log and console.error are fine for quick local debugging — they write to stdout and stderr — but production needs more: severity levels (debug, info, warn, error), timestamps, and machine-readable output that log aggregation tools can search.
Dedicated libraries provide this. Winston is flexible with multiple transports (console, file, external services) and formats; Pino is extremely fast and logs structured JSON by design. They let you set a minimum level per environment (verbose in dev, info or warn in prod) and attach context like a request id to correlate log lines.
Best practices: log structured JSON in production so it is queryable, never log secrets or full passwords/tokens, include enough context (request id, user id) to trace a request, and write logs to stdout so the platform or a container collector ships them — rather than managing log files inside the app.
Syntax
logger.info("message", { userId });Examples
JavaScript
// pino: fast, structured JSON logs
const pino = require("pino");
const logger = pino({ level: process.env.LOG_LEVEL || "info" });
logger.info({ userId: 42 }, "user logged in");
logger.error({ err }, "request failed");