INFRA-DEVOPS Contents

Structured Logging and Correlation IDs

Implement structured logs with stable fields and correlation IDs so operators can query and pivot during outages.

On this page

Structured Logging Rules

  • Log in JSON (or key=value) with stable field names.
  • Always include: timestamp, level, service, env, request_id/trace_id.
  • Never log secrets or raw tokens; redact aggressively.

Recommended Fields

  • request_id, trace_id, span_id
  • http.method, http.path, http.status, duration_ms
  • error.type, error.message (sanitized), retry_count

Example Log Line

{"ts":"2026-02-19T19:30:12Z","level":"error","service":"api","env":"prod","request_id":"8f3c...","http_method":"GET","http_path":"/v1/orders","http_status":502,"duration_ms":1840,"error_type":"upstream_timeout"}

Query Playbook (Concept)

# Find top error types in last 15m
# filter: env=prod AND service=api AND level=error
# group by: error_type, http_path
# then drill down by request_id/trace_id

Failure Modes

  • Free-text logs: hard to aggregate and search reliably.
  • Unstable field names: dashboards break and queries rot.