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
- An empty Node service
- Something to call
- Make it listen
- The problem, on purpose
- A real logger
- Fields, not sentences
- Turn the volume knob
- Log every request, once
- Switch it on
- One id per request
- Stamp it on everything
- Wire it in order
- Follow it across services
- Carry the trace too
- One way to fail
- Prove the error path