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

Comic about structured logging: three services log the same failed login for user 42 in three free-text formats, so there is nothing common to search; logs become named fields like level, msg, user_id and request_id, with the same names everywhere; one search, user_id = 42 AND level = warn, finds every matching event across services; passwords and personal data stay out of the fields.