DEV Community

Young Gao
Young Gao

Posted on

Structured Logging in Production: Why You Should Stop Using console.log (2026)

Your production logs are a wall of unstructured text. Grep fails. Debugging takes hours. You need structured logging.

The Problem

User login failed for john@example.com
Error processing payment
Connection timeout after 30s
Enter fullscreen mode Exit fullscreen mode

Which user? Which payment? Which service timed out? Unstructured logs cannot answer these questions.

Structured Logging with Pino

import pino from "pino";
const logger = pino({ level: process.env.LOG_LEVEL || "info" });

// Instead of: console.log("User login failed for " + email)
// Do: logger.warn({ userId, email, reason }, "Login failed")
Enter fullscreen mode Exit fullscreen mode

Output: {"level":40,"time":"2026-03-21T...","userId":"u_123","email":"john@example.com","reason":"invalid_password","msg":"Login failed"}

Request Context Middleware

Every log in a request should include requestId, method, path, userId:

app.use((req, res, next) => {
  req.log = logger.child({ requestId: crypto.randomUUID(), method: req.method, path: req.path });
  next();
});
Enter fullscreen mode Exit fullscreen mode

Now every log in that request automatically includes the requestId. Trace a single request across your entire system.

Log Levels Done Right

error = broken, warn = recoverable, info = business events, debug = dev context

Sensitive Data Redaction

Never log passwords, tokens, credit card numbers, or PII. Use pino redact:

const logger = pino({ redact: ["password", "token", "req.headers.authorization"] });
Enter fullscreen mode Exit fullscreen mode

Shipping Logs

JSON logs pipe directly to Datadog, Elasticsearch, CloudWatch, Grafana Loki. No parsing regex needed.

Summary

  1. Use Pino (fastest Node.js logger)
  2. Log JSON, not strings
  3. Add requestId to every log via child loggers
  4. Use correct log levels
  5. Redact sensitive data
  6. Ship to centralized logging

Part of my Production Backend Patterns series. Follow for more practical backend engineering.


If this was useful, consider:


You Might Also Like

Follow me for more production-ready backend content!


If this helped you, buy me a coffee on Ko-fi!

Top comments (0)