Lesson 1 of 9 · 24m
Structured Logging as Operational Data
Design log events as queryable operational records with stable fields, useful context, and deliberate severity instead of decorative strings.
The worst incident log I ever inherited was one line long: something bad happened. It was written by a service that processed payroll. Technically, it was accurate. Operationally, it was worthless. Logging is not the art of writing prose into a file; it is the discipline of emitting evidence that a tired engineer can query at 03:00.
Logs are events, not sentences
A log entry should describe one event with stable dimensions:
{
"ts": "2026-11-30T18:42:11.284Z",
"level": "error",
"service": "billing-api",
"env": "prod",
"version": "1.18.4",
"trace_id": "7c9b05f05e2a4b0f",
"request_id": "req_9e34a0",
"customer_id": "cus_1842",
"event": "invoice.payment.failed",
"outcome": "retry_scheduled",
"error_kind": "processor_timeout",
"duration_ms": 1204,
"attempt": 2
}That record can be counted, grouped, filtered, joined, and alerted on. Payment failed for customer cannot. Human readability matters, but put it in message after the machine-readable facts are settled.
Choose stable field names before you need them
Pick names and units once: duration_ms, not sometimes elapsed and sometimes latencySeconds; user_id, not uid in one service and account in another. A tiny schema beats a heroic parser. In Python, structlog makes this ergonomic:
import structlog
log = structlog.get_logger()
log.info(
"invoice.payment.failed",
customer_id="cus_1842",
invoice_id="in_9231",
attempt=2,
duration_ms=1204,
error_kind="processor_timeout",
)The event name carries what happened. Fields carry the dimensions. Do not interpolate high-value facts into the message where they become impossible to aggregate.
Severity is a promise about attention
Use levels consistently:
debug useful while developing or temporarily diagnosing
info normal business or lifecycle events worth retaining
warning degraded behavior, retries, approaching limits
error a unit of work failed and needs context preserved
critical service-level failure requiring immediate human attentionIf everything is an error, nothing is. If routine retries are errors, responders learn to ignore real failure. Severity is not how bad the programmer felt; it is how much operational attention the event deserves.
Log the decision points
High-value logs answer: what did the service decide, with what inputs, and what happened next? Log state transitions, external dependency outcomes, authorization denials, queue publication, retry exhaustion, and reconciliation mismatches. Do not log every loop iteration just because production feels mysterious.
Field notes
- Never log passwords, bearer tokens, session cookies, private keys, full card numbers, or reset URLs.
- Redaction is easier when secrets are fields; secrets embedded in free text escape every filter eventually.
- Include deployment version and environment. Without them, every comparison across a rollout is guesswork.
- IDs belong in separate fields. A log line containing five concatenated IDs is an archaeological dig.
- A log statement that allocates large objects or serializes huge payloads can become the outage you were trying to observe.
Was this lesson helpful?