Node.js theorytheory 0/50 · 0%
Tooling · medium

28. Logging and observability

Structured logs, log levels and why they matter in production.

Production services should log structured data (JSON) rather than free-form strings, with a level (`debug`, `info`, `warn`, `error`) so logs can be filtered, aggregated and alerted on by log-management tools.

js
function log(level, message, meta = {}) {
  console.log(JSON.stringify({ level, message, time: new Date().toISOString(), ...meta }));
}

log("info", "server started", { port: 3000 });
log("error", "rpc call failed", { method: "eth_getBalance", error: "timeout" });

Good logging includes context (request ids, user/address, timing) without leaking secrets (private keys, tokens, passwords) — a common and costly mistake is logging an entire request body that happens to contain sensitive data.

Check your understanding

  1. 1. Why prefer structured (JSON) logs over free-form strings in production?

  2. 2. What is a common, costly logging mistake?

  3. 3. What do log levels like debug/info/warn/error enable?