Quick revision
The short answer
Since Spring Boot 3.4, we enable structured logging with one property: logging.structured.format.console for the console, or logging.structured.format.file for the log file, set to a format name. Three formats are built in: ecs (Elastic Common Schema), gelf (Graylog Extended Log Format) and logstash. For anything else, we implement StructuredLogFormatter and give its class name as the format. Before 3.4, we needed an extra library such as logstash-logback-encoder and our own Logback configuration.
What to remember
In this answer, we saw that Spring Boot 3.4 and later enable structured JSON logging with logging.structured.format.console or logging.structured.format.file set to a format. The built-in formats are ecs, gelf and logstash; ECS is a good default and Boot fills its service fields from properties. Any other schema needs a StructuredLogFormatter implementation, named by its class, and before 3.4 the same result required logstash-logback-encoder and a custom Logback appender.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Since Spring Boot 3.4, we enable structured logging with one property: logging.structured.format.console for the console, or logging.structured.format.file for the log file, set to a format name. Three formats are built in: ecs (Elastic Common Schema), gelf (Graylog Extended Log Format) and logstash. For anything else, we implement StructuredLogFormatter and give its class name as the format. Before 3.4, we needed an extra library such as logstash-logback-encoder and our own Logback configuration.
Why this question is asked
Text log lines are written for people, but in production logs are read mostly by machines: a log platform has to split each line into a timestamp, a level, a message and other fields before anyone can search it. With text, that splitting relies on patterns that break whenever someone changes a format. Structured logging writes each event as JSON with named fields, so nothing has to be guessed. Interviewers ask this to check whether we know the modern, built-in way of doing it in Spring Boot. In this answer, we will recap what structured logging is, enable it, compare the three formats, write a custom formatter, and see how it was done before 3.4.
1A Quick Recap of Structured Logging
A normal Spring Boot log line is text produced from a pattern: date, level, process id, thread, logger name and message, separated by spaces. A human can read it, but a program has to parse it, and the parser breaks if the message contains the separator or if one team changes the pattern.
A structured log line is a JSON object: each piece of information is a named field. The log platform reads it directly, and every field, including ones we add, such as an order id or a trace id, becomes searchable. One JSON object per line is the usual convention, because log collectors read output line by line.
2Enabling It With One Property
In Spring Boot 3.4 and later, structured logging is part of Boot's logging support, for Logback and Log4j2 alike. We choose where it applies and which format to use.
Notice that the service name came from spring.application.name without any extra setting. The exact shape of the JSON, nested objects or dotted names, can differ between Boot versions, so check the output of your version before writing queries against it.
For a log file, we set logging.structured.format.file instead, together with logging.file.name. A common setup writes structured JSON to the console for the log platform and keeps a readable text file, or the other way round for local development. In containers, the usual choice is JSON on the console and no file at all.
3The Three Built-In Formats
Each built-in format matches a well-known log schema, so the log platform that expects it can read the fields without extra mapping. ECS is a good default when there is no strong reason for another, because many tools understand it and its field names are documented. It also has standard places for service metadata, which Boot fills from properties such as logging.structured.ecs.service.name and logging.structured.ecs.service.version, with the name defaulting to spring.application.name.
All formats include the values in the MDC, the mapped diagnostic context, a per-thread map that Micrometer Tracing uses for the trace id and span id. So once tracing is on, every log line carries its trace id as a field. Recent versions also include SLF4J key-value pairs added with the fluent API, which is the cleanest way to attach business fields.
| Format value | Schema | Typical consumer |
|---|---|---|
| ecs | Elastic Common Schema | Elasticsearch, Kibana, many OTel-based tools |
| gelf | Graylog Extended Log Format | Graylog |
| logstash | Logstash JSON event format | Logstash pipelines |
| fully qualified class name | Your own StructuredLogFormatter | Any other schema |
Notice that orderId is its own field, not just part of the message text, so a search for orderId:A-88213 finds every line about that order exactly, across every service that logs it the same way.
4Writing a Custom Format
When the log platform expects a different schema, we implement StructuredLogFormatter for the backend's event type, ILoggingEvent for Logback, and return one formatted line. Boot's JsonWriter helper builds the JSON safely, escaping quotes and control characters for us.
Notice that the format property now holds a fully qualified class name instead of ecs. Boot creates the formatter and can pass it useful objects through its constructor, such as the Environment; the exact constructor parameters that are supported are listed in the reference documentation for your version. For small changes to a built-in format, such as renaming or adding a field, recent versions also offer properties under logging.structured.json, which is simpler than a full formatter.
5How It Was Done Before 3.4
Before Spring Boot 3.4 there was no built-in support, so teams added a library, usually logstash-logback-encoder, and wrote a logback-spring.xml with a ConsoleAppender that used its LogstashEncoder. That works well and is still common in older services, but each team maintained its own configuration and field names drifted between services. With 3.4 and later, the same result is one property, and moving an older service over means removing the encoder dependency and the custom appender and setting logging.structured.format.console.
6Follow-up questions
Does structured logging change how I write log calls?
Not for ordinary messages: log.info("Order {} placed", id) still works and becomes the message field. To make values searchable as fields, I add them as key-value pairs with the SLF4J fluent API or put them in the MDC for the duration of a request.
Can I use structured JSON only in production?
Yes. Put logging.structured.format.console=ecs in a production profile file such as application-prod.properties, and keep the readable pattern for local development.
Is structured logging slower than text?
The difference is small for typical volumes; formatting JSON costs about the same as formatting a pattern. The larger effect is on the log platform, which no longer spends CPU parsing text with patterns.
7Conclusion
In this answer, we saw that Spring Boot 3.4 and later enable structured JSON logging with logging.structured.format.console or logging.structured.format.file set to a format. The built-in formats are ecs, gelf and logstash; ECS is a good default and Boot fills its service fields from properties. Any other schema needs a StructuredLogFormatter implementation, named by its class, and before 3.4 the same result required logstash-logback-encoder and a custom Logback appender.
Open with
Since Spring Boot 3.4, structured logging is built in, so I just set logging.structured.format.console, or logging.structured.format.file for the file output, to a format name. There are three built-in formats: ecs, the Elastic Common Schema; gelf, the Graylog Extended Log Format; and logstash, the Logstash JSON format.
Then, if they want more
Each log event then becomes one JSON object per line with named fields like timestamp, level, logger, thread, message and the service name taken from spring.application.name.
Values in the MDC are included, so with Micrometer Tracing the trace id and span id appear on every line, and recent versions also include SLF4J key-value pairs from the fluent API, which is how I would add business fields like an order id.
I usually pick ECS because many tools understand it, and set service version and environment through the logging.structured.ecs properties. If the log platform expects a different schema, I implement StructuredLogFormatter for ILoggingEvent, often using Boot's JsonWriter helper, and put the class name in the same property.
Before 3.4, we needed a library like logstash-logback-encoder and a custom appender in logback-spring.xml; migrating an old service means removing that and setting one property.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How do you enable structured JSON logging in Spring Boot 3.4 or later, and which formats...": https://scrivio-interview-library.netlify.app/spring-boot/boot-logging/d59e68bbd4/