Canonical log line
The one authoritative summary log written when a request ends, with its result, timing, and debugging context.
See it
What it is
A canonical log line, a pattern Stripe popularized through Brandur Leach's engineering post, is the single structured summary emitted when a request finishes. It records the outcome and the context needed to explain it, such as route, status, duration, request ID, tenant, release, and error class. It is a practical form of the wide-event pattern: one authoritative row for the whole request instead of a breadcrumb trail.
Use it when request logs are noisy but still fail to answer basic questions. Populate one context object throughout the request and write it at the completion boundary, where the final status and duration are known. Keep extra log lines for exceptional state changes, not routine narration.
Gotcha: 'one per request' is easy to violate through retries, error handlers, and both finish and close callbacks. Pick one emission path, guard it against duplicates, and test aborted requests. A canonical line also becomes useless if field names drift between routes.
Ask AI for it
Add a canonical log line to this Express service using pino. Bind a child logger to request_id and trace_id, accumulate route, method, tenant_id, user_id, release, status, duration_ms, response_bytes, database_calls, cache_status, and error_type during the request, and emit one event named http_request from res.on('finish'). Add an idempotent emission guard and handle aborted requests through res.on('close') without producing a duplicate. Redact authorization, cookies, passwords, and email, and test success, failure, abort, and concurrent request cases.