← All bricks

Node / TS · Intermediate

Production-grade Structured Logging

Turn plain console.log into JSON logs you can actually investigate — levels, request_id, trace_id and one error format — one tiny delta at a time.

Plain console.log is fine until the night an incident hits and you are grepping millions of lines for one request. Here we build a small HTTP service and, step by tiny step, turn its logging into something you can search, correlate and trust: structured JSON, log levels, a per-request logger carrying a request_id, an inbound trace_id, and one error format. Every page is one small change with a short note on what we're doing and why.

What you'll build

  • Emit structured JSON logs instead of free-form text
  • Control verbosity with log levels and an env switch
  • Attach a request_id to every log line of a request
  • Propagate an inbound trace_id across the request
  • Report all errors in one consistent format

Contents

  1. An empty Node service
  2. Something to call
  3. Make it listen
  4. The problem, on purpose
  5. A real logger
  6. Fields, not sentences
  7. Turn the volume knob
  8. Log every request, once
  9. Switch it on
  10. One id per request
  11. Stamp it on everything
  12. Wire it in order
  13. Follow it across services
  14. Carry the trace too
  15. One way to fail
  16. Prove the error path