Post

Structured Logging vs Plain Logs

Introduction

Plain text logs are easy to emit but expensive to analyze at scale. Structured logging treats logs as data, enabling reliable search, correlation, and analytics. This difference becomes critical when you are debugging distributed systems with high traffic and strict SLOs.

Why Plain Logs Break Down

Plain logs create ambiguity. The same message can be formatted differently across services, and parsing rules must be inferred after the fact. This makes it difficult to answer questions such as “which user is impacted” or “how many retries happened for one request” without bespoke parsing logic.

Benefits of Structured Logging

Structured logs are emitted as key-value pairs. They offer a stable schema, making them easier to index and query.

  • Consistent field names across services.
  • Faster search due to predictable fields.
  • Safer correlation with traces and metrics.
  • Reduced parsing costs and errors.

Node.js Example: JSON Logs with Context

The following example uses a Node logger and includes a trace identifier for cross-signal correlation. The same data can be indexed directly without parsing regex.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import pino from "pino";

const logger = pino({
  level: "info",
  base: null
});

export function logRequest(req, res, durationMs) {
  logger.info({
    msg: "request.completed",
    route: req.route?.path ?? "unknown",
    method: req.method,
    status: res.statusCode,
    duration_ms: durationMs,
    trace_id: req.headers["traceparent"] ?? "missing"
  });
}

Designing a Log Schema

Advanced teams define a shared schema and version it just like an API. Recommended fields include service, environment, trace_id, span_id, request_id, and error_kind. Avoid nested structures when your log backend has limited indexing capabilities.

Queryability and Cost Control

Structured logs can still explode in volume. Apply sampling for high-frequency success logs, keep error logs at full fidelity, and enforce retention tiers. Index only the fields that matter for alerting and debugging to control costs.

Conclusion

Structured logging is a prerequisite for production-grade observability. It reduces ambiguity, accelerates incident response, and provides the context needed for reliable, automated analysis.

This post is licensed under CC BY 4.0 by the author.