Quick reference
Structure, context and retention, for logs you can query at 3am.
- A record, not a sentence — Fields you can filter on: time, level, service, version and ids.
- Structure first — JSON or logfmt, stable names, numbers as numbers.
- Levels route — The level decides who reads the line and how long it stays.
- One id joins all — trace_id on the line links the metric, the trace and the log.
In one line: Small line, complete record. If a field is missing at 3am, it was missing at design time.
Start this week:
- Pick one service and add the six fields.
- Query it by trace id before you roll it out.
- Set retention and a volume budget in the same change.
Explained
Start with six fields: timestamp in RFC 3339 UTC, level, service, version, trace id and span id. Add the two or three values you filter on, such as tenant, route and status code. Keep names stable across services: a field renamed in one service is a field missing from the join.
Structure beats formatting. JSON parses everywhere; logfmt is shorter in a terminal. Pick one per service and keep the types honest: durations as integer milliseconds, status as a number, ids as strings.
Levels route: error means a human may act, warn means degraded but handled, info means a state change, debug means development. A retry that succeeds is not an error.
Do the arithmetic. One line at 500 bytes, at 2,000 lines a second, is 1 MB a second: 86 GB a day before replication. Retention you cannot afford is retention you do not have.
Checklist
Run it on one service before you copy the pattern to twenty.
Write
- Every line has ts, level, service and version.
- Ids are strings; durations are integers in milliseconds.
- Secrets and payloads are absent by test, not by habit.
Correlate
- trace_id and span_id appear on request logs.
- A trace id finds its logs in one query.
- The metric exemplar links to the same trace.
Keep
- Retention is set per stream and enforced.
- Debug is off in production, or sampled.
- The collector drops health checks before storage.
Review
- A new field has an owner and a query that uses it.
- Log volume and cost are on a dashboard.
- One line per event, with no duplicate emitters.
The field guide
The full guide of the PDF (P1093), page by page.
A log line is a record (the line)
The useful part of a log line is not the message. It is the fields around it: the time, the level, the service and the values you will filter on.
Write the timestamp in RFC 3339 with an explicit offset, and normalise to UTC. A local time without an offset is a puzzle, not a record, and two services in two time zones will disagree about the order of events. Keep the precision you need to order lines inside one request, then stop.
| Field | Why it earns its bytes |
|---|---|
| ts | Ordering across services and time zones |
| level | Filter and route without parsing text |
| service, version | Which deploy produced the line |
| trace_id, span_id | Join the line to the trace |
| tenant, route | Group and attribute cost |
- One line per event, not one per loop iteration.
- Key-value fields beat a formatted sentence.
- Never log a token, a password or a full card number.
- Log the decision and the inputs that changed it.
The test Take a line from last week and answer four questions: which request, which user, which deploy, which outcome. If any answer needs a regex over the message, the line is missing a field.
A log line is a row in a table you did not design. Design it anyway.
Structure beats a sentence (the shape)
JSON is the default because every language parses it, and logfmt is shorter and easier to read in a terminal; either is fine, and an unstructured message is not.
One event, as logfmt
ts=2026-10-30T09:12:44Z level=error service=checkout v=2.14.1
trace_id=4bf92f3577b34da6 span_id=00f067aa0ba902b7
tenant=acme route="POST /pay" amount=42.00 currency=GBP
msg="payment declined" code=card_declined attempt=2Keep field names stable across services. A field renamed in one service is a field missing from the join, and the join is the only reason to keep the line. Put the schema in the repository next to the code that writes it, and review a new field like a new column.
| Choice | Effect |
|---|---|
| snake_case keys | One name per concept, forever |
| Durations as integers | No float rounding in filters |
| Status as a number | Group by code, not by wording |
| Ids as strings | No precision loss on 64-bit ids |
Do not log the payload Log the shape and the identifier: field count, schema version, order id. A payload log is a data-protection incident with a timestamp, and the retention policy will keep it longer than you meant.
The message is for humans. The fields are for the query.
Levels are a routing decision (the levels)
The level is not a severity opinion. It is the switch that decides what pages, what is kept longer and what is dropped at the collector.
| Level | Retention, and what it is for |
|---|---|
| error | Long, sampled: a human may act |
| warn | Medium, sampled: degraded but handled |
| info | Short: state and business events |
| debug | Off in production |
- Log once, at the level that owns the response.
- A retry that succeeds is info, not error.
- Do not log a stack trace once per request.
- Rate-limit repeated lines before they reach storage.
The arithmetic of a noisy error If five per cent of requests log an error line and you serve 2,000 requests a second, that is 100 error lines a second before the incident starts. Check that number before you promise thirty days of retention.
Errors that repeat are a counter, not a story. Count them at the emitter, log the first occurrence with full context, and let the metric carry the volume. The log is there to explain one case, not to reproduce the whole outage.
A level is a promise about who reads the line.
Carry the context, then join (the join)
A trace id on a log line is the cheapest join in observability. It turns three tools into one investigation: the metric shows the scope, the trace shows the path, the log explains the span.
- Accept or generate a W3C trace context at the edge.
- Put trace_id and span_id on every log line in the request.
- Emit the same ids as span attributes in the tracer.
- Link a metric exemplar to the trace id where the client supports it.
- Test the join: pick a trace and find its logs by id.
One service, opentelemetry environment
OTEL_SERVICE_NAME=checkout
OTEL_EXPORTER_OTLP_ENDPOINT=http://collector:4317
OTEL_TRACES_SAMPLER=parentbased_traceidratio
OTEL_TRACES_SAMPLER_ARG=0.1A sampled trace still needs its logs If head sampling drops nine traces in ten, keep the logs for the dropped ones. Otherwise you can explain the errors you kept and not the requests that produced them.
Correlation is a schema, not a tool feature. The id has to be propagated by every service in the path, including the ones written in another language and the ones nobody has touched since the last reorganisation.
One id, three tools, one story.
Retention is a number with a bill (the cost)
Before you promise ninety days, compute the gigabytes. Log volume is easy to estimate and hard to shrink after the fact.
| Tier | Window, and what it is for |
|---|---|
| Hot | 7 days: search during an incident |
| Warm | 30 days: weekly review and trends |
| Cold | 90 days or more: audit and legal hold |
One line at 500 bytes, at 2,000 lines a second, is 1 MB a second: 86 GB a day, 2.6 TB a month, before replication and index overhead. That is the number to put in the design review, not the retention slider.
- Set retention per stream, not per cluster.
- Enable Loki compactor retention explicitly; an unset retention is not a policy.
- Sample debug and health-check lines at the collector.
- Put the cost of a new log line in the pull request.
The 3am test If the incident is forty days old, which lines do you still need? Keep those, drop the health checks, and write the answer down as the retention rule.
Retention you cannot afford is retention you do not have.
The honest caveat: structured logging adds bytes per line and a schema to maintain. It pays back the first time you filter by tenant without a regex.
Your turn: What is the one field your logs are missing today?
Related on this site
More notes: Observability & SRE
- P1101Query commands for metrics, logs, traces
- P1100Five questions for your telemetry
- P1099Cardinality and the bill nobody predicted
- P1098Turning an alert into a runbook link
- P1097Four observability numbers
- P1096Dashboards for three audiences
- P1095Trace sampling: head, tail, always-on errors
- P1094Five alert anti-patterns
Open in the notes app: search, filters, copy This note on GitHub