# Structured Logging

> Emit machine-readable logs with context so you can search, filter and correlate them during an incident. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/structured-logging
Last updated: 2026-11-28

Emit machine-readable logs with context so you can search, filter and correlate them during an incident.

Intermediate · about 3 hours · difficulty 2/5

Learn first: [Twelve-Factor Apps and Cloud-Native Habits](https://www.internhack.in/roadmaps/tracks/backend/twelve-factor-apps)

Logs are the first thing you open when something breaks. `console.log("error!")` scattered around the code will not help at 3 a.m. with ten instances running. Good logging is deliberate.

### Structure, not prose

Write logs as **JSON objects** with consistent fields instead of free text: `{"level":"error","msg":"payment failed","orderId":42,"provider":"stripe","requestId":"a1b2","durationMs":812}` Now you can query "all errors for order 42" or "p95 duration by provider". Libraries: **pino** or winston (Node), structlog or the stdlib `logging` with a JSON formatter (Python), `slog` or zap (Go), Logback/SLF4J (Java), Serilog (.NET).

### Levels and context

- Use levels meaningfully: `debug` for development detail, `info` for notable business events, `warn` for recoverable oddities, `error` for failures needing attention.
- Attach a **request id** (or trace id) to every log line in a request, usually via middleware and async context, so you can follow one request across services.
- Log at boundaries: incoming request, outgoing calls, job start and end, and every caught error with its stack.

### Where logs go

Following twelve-factor, write to **stdout** and let the platform ship logs to a central store: Loki, Elasticsearch/OpenSearch, CloudWatch, Datadog and similar. Set **retention** and sampling to control cost.

### Pitfalls

- Logging secrets, tokens, passwords or full card numbers. Redact fields centrally.
- Logging personal data you are not allowed to retain.
- So much noise at `info` that real problems drown; review logs like code.

You are ready to move on when every request in your app produces JSON logs with a request id, errors include stack traces, and sensitive fields are redacted automatically.

## Key concepts

- **JSON structured logs**
- **Log levels**
- **Request and correlation ids**
- **Redacting sensitive data**
- **Centralized log storage**
- **Retention and sampling**

## Mini project

Add structured logging with a request id middleware to your API, redact auth headers and passwords, ship logs to a local Loki or OpenSearch and query all logs for one failing request.

## Check yourself

Why is a request id in every log line so valuable when you have many instances or services?

## Free resources

- [OWASP Logging Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Logging_Cheat_Sheet.html) (OWASP)
- [The Twelve-Factor App: Logs](https://12factor.net/logs) (12factor.net)
- [Pino documentation](https://getpino.io/) (Pino)

Previous: [Server-Sent Events and Polling](https://www.internhack.in/roadmaps/tracks/backend/sse-and-polling) · Next: [Metrics, Dashboards and Alerting](https://www.internhack.in/roadmaps/tracks/backend/metrics-alerting)

## More in Observability and Performance

- [Structured Logging](https://www.internhack.in/roadmaps/tracks/backend/structured-logging)
- [Metrics, Dashboards and Alerting](https://www.internhack.in/roadmaps/tracks/backend/metrics-alerting)
- [Distributed Tracing with OpenTelemetry](https://www.internhack.in/roadmaps/tracks/backend/distributed-tracing-otel)
- [Performance Profiling and Load Testing](https://www.internhack.in/roadmaps/tracks/backend/performance-profiling)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
