Logging
We make Wren’s logs into records a machine can search, so that a support ticket quoting a request id leads straight to what happened. A log line that only a human can read stops being useful at the first thousand requests a second. We will cover structured logs and levels, request and correlation ids, centralized logging with sampling and retention, and keeping personal data and secrets out of logs.
#Section 1Structured logs and log levels
A support agent forwards a complaint. User 42 says their order failed at about ten past eight last night. The first version of Wren’s payments service logged lines like ERROR payment failed for user 42: card declined. Searching for that across four instances means a text search through millions of lines, guessing which wording each developer used, and hoping the user id was included. Free text was written for a person reading one terminal. It does not scale to a fleet.
A structured log is a record with named fields, usually one JSON object per line. The same event becomes {"ts":"2026-10-05T20:10:42.118Z","level":"error","service":"payments","request_id":"req_7Hq2","user_id":42,"order_id":124,"event":"payment_failed","reason":"card_declined","provider_status":402,"duration_ms":311}. Now a query such as service=payments AND event=payment_failed AND user_id=42 finds it in milliseconds, a dashboard can count failures by reason, and an alert can fire when provider_status=500 passes a threshold. The rule is to log events with stable names and put the variable parts in fields, never inside the message text.
Log levels say how much attention a line deserves. The usual scale is DEBUG for detail only useful while developing, INFO for normal significant events such as “order created”, WARN for something unexpected that the service handled, such as a retry that succeeded, ERROR for a failed operation that someone may need to look at, and FATAL for a failure that stops the process. Production runs at INFO, and the level can be raised for one service or one request at runtime when debugging, without a deploy.
Levels go wrong in two predictable ways. If every handled 404 is logged as ERROR, the error count becomes noise and nobody reads it. If a real failure is logged at INFO, nothing alerts. Wren’s convention is that ERROR means “this request failed because of us”, so a 4xx from a client mistake is INFO with the status in a field, and a 5xx is ERROR with the exception and stack trace attached as fields. Each log line also carries the service, version, instance and environment, added automatically by the logging library, so a line can always be traced to the exact build that wrote it. Libraries such as structlog in Python, zap and slog in Go, and pino in Node.js write JSON by default and make it cheap.
#Section 2Request ids, correlation and context
A single click by user 42 produces log lines in the gateway, the order service, the payments service, a Kafka consumer and an email worker. Each line is structured, but without a shared key they are five unrelated records. A request id is that key. Wren’s gateway generates one for every incoming request, req_7Hq2, unless a trusted upstream already set one, puts it in an X-Request-Id header on every call it makes, returns it to the client in the response, and writes it into every log line. Unit V showed it in error bodies, so a customer can quote it to support.
A correlation id is the same idea stretched across asynchronous work. When the order service publishes order-created, the event carries the request id in its headers, Unit VIII, and the consumer logs it, so the email sent two seconds later still connects to the click that caused it. When a batch job processes many orders, each item gets its own id with a field pointing back to the job. The names vary between teams. What matters is that any line can be joined to its cause. Part IV replaces the hand-rolled request id with a trace id from distributed tracing, which carries the same information plus the shape of the call tree, and most teams log both.
Passing ids by hand through every function call does not survive a large codebase. Contextual logging stores the request’s fields once, at the start of the request, in context that the logging library reads automatically. In Python that is a contextvars variable, in Node.js AsyncLocalStorage, in Go a context.Context passed down the call chain, and in Java the MDC, the mapped diagnostic context. The middleware that authenticates the request binds request_id, user_id, tenant_id and the route, and every later log call includes them without the developer writing them. A background worker binds the job id and the correlation id from the message in the same way.
The payoff shows in the support ticket. The agent pastes req_7Hq2 into the log search and gets eleven lines from four services in time order, ending with the payment provider’s 402 and the reason card_declined. That took one query, and no one had to guess which instance handled the request or what words the developer had chosen.
#Section 3Centralized logging, sampling and retention
Logs written to a file on an instance disappear when the instance is replaced, and Kubernetes replaces pods all the time, Unit XV. So production logs are shipped off the machine. The twelve-factor convention is for the application to write to standard output and let the platform collect it. A collector such as Fluent Bit, Vector or the OpenTelemetry Collector runs on each node, reads every container’s output, adds metadata such as the pod and node names, and sends batches to a central store, centralized logging. The store is often Elasticsearch or OpenSearch, Grafana Loki, or a hosted service such as Datadog or CloudWatch Logs.
Volume decides cost. Wren handles 43,200,000 requests a day, and each request writes about 5 lines of 400 bytes, 2,000 bytes per request. That is 86.4 GB of logs a day, and 2.592 TB over a 30-day retention period, before indexing overhead, which can double it in full-text stores. Hosted logging is usually priced per gigabyte ingested, so every line has a price.
Sampling reduces volume where detail is repetitive. Wren keeps every WARN and ERROR, every line from requests slower than 1 second, and every line for requests marked as debug, but keeps only 10% of INFO lines from successful fast requests, chosen by hashing the request id so that all lines of a sampled request are kept together. That cuts volume by roughly 80% without losing the requests anyone investigates. Counting should never depend on sampled logs, which is the job of metrics, Part II.
Retention is a policy, not a default. Hot, searchable storage is expensive, so Wren keeps 14 days hot for debugging, moves logs to cheap object storage for a year for audits, and keeps security audit events, logins, permission changes and admin actions, for longer in a separate append-only store that application credentials cannot delete. Legal requirements cut the other way. Personal data in logs is subject to privacy law, such as the GDPR in Europe, so logs must not keep it longer than needed, which the next section addresses.
#Section 4Redaction, personal data and secrets
Logs are copied more widely than any database. They go to a vendor, to engineers’ laptops during an incident, into tickets and chat. That makes them one of the most common places for secrets and personal data to leak. In 2018, both Twitter and GitHub disclosed that bugs had written some users’ plaintext passwords to internal logs, and both asked users to change their passwords. Neither system was breached from outside. The passwords had simply been logged.
Wren classifies what may appear in logs. Identifiers, such as user id 42 and order id 124, are fine and necessary. Personal data, information about an identifiable person such as names, emails, phone numbers, addresses and IP addresses, is kept out or reduced, for example logging only the email’s domain or a hash. Secrets, passwords, tokens, API keys, session cookies, card numbers and authorization headers, never appear at all. Card numbers are a compliance matter as well, since the PCI DSS standard forbids storing full card numbers outside tightly controlled systems, and a log file counts as storage.
Prevention works at several layers. The first is not logging whole objects. Code that logs request.headers or user will one day log an Authorization header or a new password_reset_token field someone adds next year. Wren logs chosen fields only. The second is a redaction filter in the logging library that replaces values of known sensitive keys, password, token, authorization, cookie, secret, card_number, with [REDACTED] before the line is written, as a safety net. The third is pattern scanning in the pipeline that detects things shaped like secrets, such as Wren’s API keys, which start with a recognisable wren_live_ prefix, card numbers that pass the Luhn checksum, and JWTs, which start with eyJ, and masks them and raises an alert, because a match means a code path is leaking.
Access controls finish the job. Log access is granted by role, production logs are not copied to laptops, and searches are themselves logged. When a secret does appear in logs, the response is to rotate the secret, not to delete the lines, because the lines may already have been copied to places nobody can reach.