Structured logging means writing each log event as a set of named fields, usually one JSON object per line, instead of a free-form sentence. In a microservices system this matters because one user request crosses many services, and only machine-readable logs with a shared request or trace identifier let you search, join and aggregate those events reliably. Structured logs are what make log platforms, dashboards and alerts practical at scale.
An unstructured line looks like this:
To find slow payments you need a regular expression, and any wording change breaks it. The same event as a structured record:
Every value is a field you can filter, sort and aggregate without parsing text.
trace_id lets you pull every event for one request from the gateway, the order service and the payment service in a single query.level, service, user_id or error.type instead of grepping text.duration_ms and status can feed dashboards and alert rules.Agree on a small schema and enforce it in a shared library or in your logging configuration.
| Field | Purpose |
|---|---|
timestamp | UTC time in ISO 8601 format |
level | Severity such as INFO, WARN, ERROR |
service, version, env | Which deployment produced the event |
trace_id, span_id | Link to distributed traces |
message | Short, constant description of the event |
error.type, error.stack | For failures only |
| Business fields | Order ID, tenant, amount and similar |
Keep the message text constant and put variable data into fields. Never log passwords, tokens, full card numbers or other personal data, and mask or omit sensitive values before they reach the logger.
Spring Boot 3.4 added built-in structured logging. Set one property and the console output becomes JSON:
You can add your own fields through the SLF4J key-value API, available in SLF4J 2:
For request-wide fields such as the trace ID, use the MDC (mapped diagnostic context). With Micrometer Tracing or the OpenTelemetry Java agent, the trace and span IDs are placed in the MDC automatically, so they show up in every JSON event without extra code. On older Spring Boot versions, use logstash-logback-encoder to get JSON output from Logback.
A common pipeline is: each service writes JSON to standard output, the container platform collects it, and an agent forwards it to a central store.
stdout in JSON. Do not write log files inside containers.Then a query like trace_id = "4bf92f35..." returns the full path of one request. Structured logs complement, and do not replace, metrics and traces: use metrics for trends, traces for timing across services and logs for the detail of individual events.
DEBUG in production by default.No, but it is the most widely supported. Logfmt (key=value pairs) is also used, particularly with Loki. Pick one format across all services.
JSON encoding adds some CPU cost, which is small compared with I/O in most services. Use asynchronous appenders for high-volume logging if measurement shows an impact.
Propagate a trace context, ideally the W3C traceparent header, and include the trace ID in each log event. OpenTelemetry and Micrometer Tracing do this for you.
Yes. Start by switching the output format and adding the trace ID, then migrate individual log statements to key-value fields over time.
Structured logging turns log lines into queryable data. In microservices it makes cross-service correlation, fast debugging and log-based alerting possible. Define a shared schema, emit JSON with trace IDs, keep sensitive data out and send logs to a central platform alongside metrics and traces.