Sub topic 12
Structured logging for log aggregation
This chapter assumes you can write log statements with SLF4J, set properties in application.properties, and have read the earlier chapters on log patterns and MDC. It covers how to make Spring Boot write logs as JSON so a log aggregation tool can parse them.
Why plain text is hard to aggregate
A log aggregator collects logs from many application instances into one searchable store. Tools such as Elasticsearch, Loki, and Splunk work best when each log event is a set of named fields. A pattern-based line like this one needs a parsing rule before you can search by level or request ID:
11:05:15.195 ERROR [a1b2c3] com.example.orders.OrderService - Order 42 failedStack traces make this worse. A trace spans many lines, and a parser can mistake each line for a separate event.
Structured logging writes each event as one JSON object. The fields are already named, and the stack trace sits inside a single field.
Enabling structured output
Spring Boot 3.4 and later can produce structured output without extra dependencies. Set a format name for the console, the file, or both.
management.endpoints.web.exposure.include=health,loggers
# Spring Boot 3.4 and later
logging.structured.format.console=ecsThe first line is not part of structured logging. It exposes the Actuator loggers endpoint, which a later chapter uses.
Spring Boot supports three formats:
| Format name | Standard | Typical consumer |
|---|---|---|
| `ecs` | Elastic Common Schema | Elasticsearch and Kibana |
| `gelf` | Graylog Extended Log Format | Graylog |
| `logstash` | Logstash JSON | Logstash and compatible tools |
To write JSON to a file as well, set logging.structured.format.file to one of the same names.
With ecs, one event looks roughly like this. Field order and extra fields vary by version and configuration:
{"@timestamp":"2026-12-01T11:05:15.195Z","log.level":"ERROR","message":"Order 42 failed","service.name":"orders","log.logger":"com.example.orders.OrderService","ecs.version":"8.11"}Set spring.application.name and Spring Boot fills in service.name, so an aggregator can tell your services apart. You can add more service fields with logging.structured.json.add.* properties.
MDC values become fields
The MDC entries you set in the previous chapter appear in the JSON automatically. With the RequestIdFilter in place, each event from a request carries requestId as a field. In the aggregator you can then filter on that one value and see every line from a single request, across all instances.
Practical points
- Write to the console in containers. The platform captures standard output, and the collector reads it from there.
- Do not switch formats halfway through a deployment without telling the aggregator. Field names change between
ecsandlogstash, which breaks saved queries and dashboards. - Keep the human-readable pattern for local development. Use a profile-specific properties file, such as
application-prod.properties, to turn on the structured format only in production. - On Spring Boot versions before 3.4, there is no built-in support. Use a JSON encoder library with a
logback-spring.xmlfile instead.
Exercise
Your application runs in a container and logs are shipped to Elasticsearch. Which property turns on the matching JSON format, and why enable it only in the prod profile?
Show answer
Set logging.structured.format.console=ecs, for example in application-prod.properties. ECS is the format Elasticsearch and Kibana expect. Plain text is easier to read in a local terminal, so the structured format is enabled only where an aggregator consumes the logs.