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
Feature | Winston | Pino |
---|---|---|
Performance | Good | Very high |
JSON logs | Yes | Yes (default) |
Transports / Output | Multiple (console, files, HTTP) | Few (mostly console, streams) |
Log levels | info, error, warn, debug | info, error, warn, debug |
Integration with frameworks | Express middleware available | Express middleware available |
Ecosystem | Large | Modern, 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
-
Never rely on console.log in production
-
Winston → flexible, good for complex logging setups
-
Pino → fast, JSON-focused, great for high-performance apps
-
Combine log levels, transports, and middleware for structured logging
-
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?