Pcornel
Browse topics
Options
Recent
Favorites
Observability · Technical reference

Structured Logging

Direct answer

Structured logging writes logs as key-value data (usually JSON) instead of free text, so fields can be filtered, aggregated and correlated.

When to use this

Use it when designing application logging.

First 30 seconds

1. Sample line is valid JSON: tail -1 app.log | jq ..

2. Required fields present.

3. trace_id included.

4. No secrets in logs.

5. Query by field works in the log platform.

Operational verification

event + context → JSON line → parsed fields → queries

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
OpenTelemetry / Prometheus / Grafana / common metrics-log-trace pipelines
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformOpenTelemetry / Prometheus / Grafana / common metrics-log-trace pipelines
Scope / prerequisiteUse it when designing application logging.
What next?2 suggested next steps
Evidence0
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 How to verify Verify boundary

Confirm timestamps are UTC with time zone and milliseconds. Confirm log levels are used consistently.

Step 2 Common failures Narrow the fault

Multi-line stack traces break line-based collectors unless joined. Inconsistent field names (userId, user_id) make queries miss data. Debug logging left on in production costs storage and performance.

Key points

Use consistent field names (timestamp, level, service, message, trace_id). Log the event once with context instead of many lines. Never log passwords, tokens or full card data.

Key checks
  • tail -1 app.log | jq . — first check for this topic
  • curl -s localhost:9090/-/ready — Prometheus ready
  • curl -s 'localhost:9090/api/v1/query?query=up' — scrape targets up/down
  • promtool check config prometheus.yml — config valid
ReferenceWhat it is1 section · depth on demand

How it works

Structured loggers serialise fields at the source. Collectors forward them without regex parsing. Shared trace IDs let you jump from a trace to its logs.

ReferenceImportant terms & boundaries3 sections · depth on demand

Example

{"ts":"2026-11-01T09:14:03.120Z","level":"error","service":"checkout","trace_id":"4bf92f3577b34da6","user_tier":"pro","msg":"payment declined","provider":"stripe","latency_ms":812}

Illustrative output, not captured Evidence. Use consistent field names (OpenTelemetry semantic conventions or ECS), include trace/span IDs for correlation, log in UTC, and never log secrets or full personal data.

In depth

One event, one line, machine-readable fields

{"timestamp":"2026-11-27T09:14:03.120Z","severity":"ERROR","service.name":"checkout",
 "trace_id":"4bf92f3577b34da6a3ce929d0e0e4736","span_id":"00f067aa0ba902b7",
 "event":"payment.declined","order_id":"A-10293","provider":"nets","http.status_code":402,
 "duration_ms":812,"message":"payment declined by provider"}

Field conventions that pay off

PracticeWhy
Timestamp in UTC, RFC 3339 with milliseconds and ZSorts and correlates across hosts
A stable event name per kind of eventCount and alert on events without regex on messages
trace_id / span_id from the active contextJump from log to trace and back
Same field names everywhere (user.id, not userId here and uid there)One query works for all services; OpenTelemetry semantic conventions give names
Variable data in fields, constant text in message"payment declined" stays groupable; the amount is a field
Durations as numbers with the unit in the nameduration_ms: 812, not "812ms"

Severity mapping

The OpenTelemetry log data model defines SeverityNumber ranges: 1–4 TRACE, 5–8 DEBUG, 9–12 INFO, 13–16 WARN, 17–20 ERROR, 21–24 FATAL. Mapping each logging library's levels onto these makes "errors across all services" one query.

What must not be logged

Passwords, tokens, session cookies, full card numbers and personal data beyond what the purpose requires. Redact at the source (logger configuration or a wrapper) and add a second filter in the collector, because a log line, once shipped, is copied into many systems.

Multi-line output

Stack traces should be one event: log them as a field (exception.stacktrace) in JSON, rather than relying on collectors to join lines with regex rules.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.

Common mistakes

  • Logging secrets.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
structured loggingstructured logging jsonlog correlation trace id
Confirmation

Confirm action

Continue?