Services

Resources

Company

/

Structured logging

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

Diagram of structured logging: the Go call slog.Warn("login failed", "user_id", 42, "request_id", "7f3a9c") becomes one JSON object per line, drawn as a table of fields: level warn, msg login failed, user_id 42, request_id 7f3a9c. A red token row is struck out with the note to keep secrets out. The query user_id = 42 AND level = "warn" fans out to services A, B and C: one search, 3 services, no regex.

Putting this into practice?

Talk to One2N about our SRE consulting

Talk to One2N about our SRE consulting