Quick revision
The short answer
When Micrometer Tracing is on the classpath, Spring Boot puts the current traceId and spanId into the logging MDC automatically, and recent Boot versions add them to the default log pattern through logging.pattern.correlation. For a log platform, Boot 3.4 and later can write structured JSON with logging.structured.format.console set to ecs, logstash or gelf. The backend can then join logs and traces by trace id; just remember that MDC values do not follow work to other threads without context propagation.
What to remember
In this answer, we saw that Spring Boot puts traceId and spanId into the MDC automatically when Micrometer Tracing is present, and that recent versions print them through logging.pattern.correlation in the default pattern. Boot 3.4 and later can write JSON logs with logging.structured.format.console, in ecs, logstash or gelf layouts, so the log platform gets fields instead of text to parse. With the trace id as a field, backends link logs and traces both ways, as long as context propagation keeps the MDC filled on other threads.
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.
When Micrometer Tracing is on the classpath, Spring Boot puts the current traceId and spanId into the logging MDC automatically, and recent Boot versions add them to the default log pattern through logging.pattern.correlation. For a log platform, Boot 3.4 and later can write structured JSON with logging.structured.format.console set to ecs, logstash or gelf. The backend can then join logs and traces by trace id; just remember that MDC values do not follow work to other threads without context propagation.
Why this question is asked
During an incident, a trace shows us which service was slow and a log line tells us why. Joining the two is only easy if every log line carries the trace id, and if the log platform can read that id as a field instead of digging it out of free text. Interviewers ask this at a junior level because it is practical knowledge every Spring Boot developer needs once a team adopts tracing. In this answer, we will recap the MDC, see how trace ids get into log lines, switch to JSON logs, look at how the backend joins logs and traces, and finish with the threading catch.
1A Quick Recap of the MDC
MDC stands for Mapped Diagnostic Context. It is a map of key-value pairs that the logging library, SLF4J with Logback by default in Spring Boot, keeps per thread. Anything we put in the MDC can be printed in every log line written by that thread, without passing it to each log call. It is the standard way to attach request-wide information such as a user id or a trace id to log lines.
2How Trace Ids Get Into Log Lines
With Micrometer Tracing and a bridge on the classpath, the tracing handler updates the MDC whenever a span becomes current and restores it when the span ends. It uses the keys traceId and spanId. So inside a request, every log line on the request thread can print them, and we write no code for it.
In older Boot versions we had to add %X{traceId} to the pattern ourselves. Since Boot 3.2, the default console and file patterns include a correlation part controlled by logging.pattern.correlation, which prints the ids when tracing is present and nothing when it is not. The exact layout depends on the version, so the line below is an example.
3Producing JSON Logs
Plain text lines are made for people. A log platform must parse them back into fields, usually with regular expressions that break as soon as a message contains an unexpected character or a stack trace spans many lines. Structured logging writes each event as one JSON object with named fields, so nothing needs parsing.
Spring Boot 3.4 added built-in structured logging. We choose a format for the console, the file or both. ecs is the Elastic Common Schema, logstash is the layout of the logstash-logback-encoder, and gelf is the Graylog format. MDC entries, including traceId and spanId, are included as fields.
Notice that the stack trace of an exception becomes a field inside the same object, so a 60-line stack trace is still one event. For applications on Boot versions before 3.4, the logstash-logback-encoder library in logback-spring.xml gives a similar result.
4Joining Logs and Traces in the Backend
Once the trace id is a field, the backend can link the two signals. In Grafana, a tracing data source can jump from a span to a log query filtered by that trace id; in Kibana or a vendor tool, the trace view shows the matching log lines. Going the other way, a log line with an error offers a link to its trace.
This only works if the field name and format match what the backend expects, which is why teams agree on one format across services. Mixing traceId in one service and trace_id in another breaks the link silently.
5 messages in order. Play them, or step through them one at a time.
The trace id is the key both systems share.
5The Threading Catch
The MDC is per thread. When work moves to another thread, for example with @Async, a CompletableFuture on an executor or a reactive pipeline, the new thread has its own empty MDC and the log lines lose their trace id. The fix is context propagation: decorate executors with ContextPropagatingTaskDecorator so the tracing context, and with it the MDC, is restored on the worker thread, and for Reactor enable automatic context propagation. A log line without a trace id inside async code is the usual sign that this is missing.
6Follow-up questions
Do we need to clear the MDC ourselves for trace ids?
No. The tracing integration sets and restores traceId and spanId as spans start and end. We only clean up keys we put there ourselves, ideally with MDC.putCloseable in a try-with-resources block so they cannot leak into the next request on the same thread.
Which structured format should we choose?
The one our log platform reads natively. ecs fits Elasticsearch and Kibana well, logstash is a common neutral layout many shippers understand, and gelf suits Graylog. Consistency across services matters more than the choice itself.
Should we keep human-readable logs for local development?
Yes. A common setup is plain text on the console for local runs and JSON in deployed environments, switched with a Spring profile. Developers keep readable output while the platform gets fields.
7Conclusion
In this answer, we saw that Spring Boot puts traceId and spanId into the MDC automatically when Micrometer Tracing is present, and that recent versions print them through logging.pattern.correlation in the default pattern. Boot 3.4 and later can write JSON logs with logging.structured.format.console, in ecs, logstash or gelf layouts, so the log platform gets fields instead of text to parse. With the trace id as a field, backends link logs and traces both ways, as long as context propagation keeps the MDC filled on other threads.
Open with
When Micrometer Tracing with a bridge is on the classpath, Spring Boot's tracing integration puts traceId and spanId into the logging MDC whenever a span is current, so every log line on that thread can include them without any code.
Then, if they want more
In older versions I'd add %X{traceId} to the pattern myself, but since Boot 3.2 the default pattern has a correlation section, controlled by logging.pattern.correlation, that prints the ids when tracing is present. For a log platform I'd switch to structured JSON.
Since Boot 3.4 that's built in: logging.structured.format.console set to ecs, logstash or gelf, and MDC values come out as fields, with stack traces inside the same event, so there's no fragile regex parsing. On older versions I'd use the logstash Logback encoder.
Once the trace id is a field, the backend can join logs and traces: from a slow span I can open its log lines, and from an error log I can open the trace. That needs every service to use the same field names.
One catch is threads: the MDC is thread-local, so with @Async or other executors I'd use a ContextPropagatingTaskDecorator, and for Reactor automatic context propagation, otherwise async log lines lose their trace id.
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 Cloud documentation, Resilience4j documentation
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 trace ids appear in log lines in a Spring Boot 3 or later application, and how...": https://scrivio-interview-library.netlify.app/spring-boot/observability/0ebefa975b/