Services

Resources

Company

Structured logging

Structured logging is writing each log event as a set of named fields, usually as JSON, rather than as a free-text sentence. A field such as user_id=42 can be filtered, grouped and counted exactly, while text needs fragile regular expressions. It is a building block of observability and makes logs easy to join with traces and metrics.

How it works

The application calls a logger with a message and key-value pairs, and the logger emits one JSON object per line. In Go, slog.Warn("login failed", "user_id", 42, "request_id", "7f3a9c") produces a line with level, msg, user_id and request_id fields. The log backend indexes those fields. A query such as user_id = 42 AND level = "warn" then finds every matching event across 3 services in one search, without parsing text.

Why it matters in production

During an incident, structured logs turn "grep and hope" into precise queries and dashboards. Standardise field names across services, include a correlation ID and trace ID, and keep high-cardinality or sensitive values such as tokens and personal data out of the fields.

See also

On the One2N blog

Further reading

Four-panel comic about Structured logging: Asha greps free-text logs: user 42 failed login, login fail for uid=42. Every service says it differently; Structured logging writes each event as named fields in JSON: level, msg, user_id, request_id; slog.Warn("login failed", "user_id", 42) becomes a...