Perfect — let’s go through logging in Node.js, focusing on Winston and Pino, which are essential for debugging, monitoring, and production observability.


1️⃣ Why Logging Matters

  • Helps debug issues during development

  • Monitors production performance and errors

  • Enables audit trails and metrics for compliance or analysis

  • Better than console.log because:

    • Supports log levels (info, error, debug, warn)

    • Can output to files, databases, or remote services

    • Can format logs consistently


2️⃣ Winston

  • Popular flexible logging library for Node.js

  • Supports multiple transports (console, file, HTTP)

  • Supports log levels, formatting, and JSON output

a) Install

npm install winston

b) Basic Example

const winston = require("winston");
 
const logger = winston.createLogger({
  level: "info",
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json()
  ),
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: "combined.log" }),
    new winston.transports.File({ filename: "errors.log", level: "error" })
  ],
});
 
logger.info("Server started successfully");
logger.error("Something went wrong");
  • transports → where logs go

  • level → minimum severity to log

  • format → can be JSON, simple text, colorized, etc.


3️⃣ Pino

  • High-performance logger for Node.js

  • Optimized for low overhead in production

  • JSON logs by default, great for log aggregation tools

a) Install

npm install pino

b) Basic Example

const pino = require("pino");
const logger = pino({ level: "info" });
 
logger.info("Server started successfully");
logger.error("Something went wrong");
  • Simple and very fast

  • Can be combined with pino-pretty for readable logs in development:

npm install pino-pretty
node app.js | pino-pretty

4️⃣ Advanced Features

FeatureWinstonPino
PerformanceGoodVery high
JSON logsYesYes (default)
Transports / OutputMultiple (console, files, HTTP)Few (mostly console, streams)
Log levelsinfo, error, warn, debuginfo, error, warn, debug
Integration with frameworksExpress middleware availableExpress middleware available
EcosystemLargeModern, optimized for speed

5️⃣ Logging Middleware for Express

With Winston

app.use((req, res, next) => {
  logger.info(`${req.method} ${req.url}`);
  next();
});

With Pino

const pinoHttp = require("pino-http");
app.use(pinoHttp({ logger }));
  • Automatically logs request/response info

6️⃣ Key Takeaways

  1. Never rely on console.log in production

  2. Winston → flexible, good for complex logging setups

  3. Pino → fast, JSON-focused, great for high-performance apps

  4. Combine log levels, transports, and middleware for structured logging

  5. Logs can be sent to files, monitoring systems, or log aggregators


Next, we could cover Monitoring & Performance Tools in Node.js, including APM tools like New Relic, Datadog, and Prometheus for production-ready applications.

Do you want to go there next?