Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

Explain this Logback pattern and what each part prints: %d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} %-5level [%thread] %logger{36} [%X{traceId}] - %msg%n%ex. Which parts are expensive?

The pattern prints an ISO-8601 timestamp with milliseconds and offset, the level padded to five characters, the thread name, the logger name shortened to about 36 characters, the traceId from the MDC, the message, a line separator and then any exception's stack trace. Almost all of it is cheap; the expensive parts would be caller data such as %class, %method, %line and %file, which this pattern rightly leaves out, and the multi-line stack trace is a parsing problem rather than a CPU problem.

Why this question is asked

A pattern layout is the first thing we touch when we set up logging, and the last thing we look at when something goes wrong, because every line we search through during an incident was shaped by it. Interviewers like this question because it checks whether we know what each conversion word really prints and, more importantly, which ones quietly cost CPU on every single log call. In this answer, we will take the pattern apart piece by piece, see an example line it produces, look at which conversion words are expensive and why, and finish with the problem that multi-line stack traces cause for log shipping.

1A Quick Recap of Pattern Layouts

In Logback, an encoder turns each logging event into bytes, and the most common encoder uses a PatternLayout. The pattern is a template: plain text is copied as it is, and every word that starts with a percent sign is a conversion word that Logback replaces with a piece of the event. Some conversion words take an option in braces, like %d{format} or %logger{36}, and some take a format modifier between the percent sign and the word, like %-5level.

A logging event carries the timestamp, the level, the logger name, the thread name, the message template and its arguments, an optional exception, and a copy of the MDC. The MDC, or Mapped Diagnostic Context, is a per-thread map of key-value pairs that our code fills in, for example a traceId set at the start of a request, so that every line logged during that request can carry it.

2Walking Through Each Part

Let us take the pattern from left to right and see what one call prints.

Java
// pattern: %d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} %-5level [%thread] %logger{36} [%X{traceId}] - %msg%n%ex
MDC.put("traceId", "4bf92f3577b34da6");
log.info("Order {} placed", 1042);
// 2026-11-05T14:03:27.481+05:30 INFO [http-nio-8080-exec-3] c.s.o.f.service.OrderService [4bf92f3577b34da6] - Order 1042 placed
log.warn("Stock low for {}", "SKU-7");
// 2026-11-05T14:03:27.502+05:30 WARN [http-nio-8080-exec-3] c.s.o.f.service.OrderService [4bf92f3577b34da6] - Stock low for SKU-7
What each conversion word prints
PartPrintsCost
%d{...SSSXXX}2026-11-05T14:03:27.481+05:30low, formatter caches per second
%-5levelINFO followed by one spacevery low
[%thread][http-nio-8080-exec-3]very low
%logger{36}c.s.o.f.service.OrderServicelow, abbreviation cached
[%X{traceId}][4bf92f3577b34da6]low, map lookup
%msgOrder 1042 placedformats arguments once
%exstack trace, many linesonly when an exception is logged
Nothing in this pattern needs the caller's stack frame.

%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} prints the event time. The text in single quotes, 'T', is a literal letter, SSS is milliseconds and XXX is the offset from UTC written as +05:30, or Z for UTC itself. The result is ISO-8601, which every log platform parses without help.

%-5level prints the level. The 5 sets a minimum width and the minus sign means left-justify, so INFO gets one trailing space and every message starts in the same column. That is why INFO and WARN lines line up above.

[%thread] prints the thread name in brackets, here a Tomcat worker thread. %logger{36} prints the logger name, usually the class name, abbreviated so that it fits in about 36 characters: Logback shortens the leftmost package segments to their first letter first and keeps the rightmost part, the class name, whole. So com.shop.order.fulfilment.service.OrderService, 46 characters, became c.s.o.f.service.OrderService; a name already shorter than 36 characters is printed in full.

[%X{traceId}] prints the MDC value for the key traceId, or nothing if it is not set. %msg is the message with its {} placeholders replaced by the arguments. %n is the platform line separator, a newline on Linux. %ex prints the stack trace of the exception passed to the call, if there is one, and prints nothing otherwise.

3Which Parts Are Expensive

Most of this pattern is cheap. The date formatter caches the formatted text and only rebuilds the part that changed, the logger abbreviation is computed once per logger and reused, and the MDC lookup is a map read. %msg formats the arguments, which costs something, but it happens once per event and we need it anyway.

The expensive conversion words are the ones not in this pattern: %class (or %C), %method (%M), %line (%L), %file (%F) and %caller. They print where in our code the log call was made. Java does not hand that information to a library, so Logback creates a Throwable on the calling thread, walks its stack trace, and finds the first frame outside the logging framework. Capturing a stack trace for every event costs microseconds instead of nanoseconds, which on a service logging a few thousand lines per second is a measurable share of CPU.

Java
// Same call with caller data in the pattern
// pattern: %-5level %class.%method:%line - %msg%n
log.info("Order {} placed", 1042);
// INFO com.shop.order.fulfilment.service.OrderService.place:57 - Order 1042 placed
// cost: a new Throwable and a stack walk for every event, even when nobody reads the line

So the good news is that the pattern in the question already avoids the expensive parts. If a team wants to know the line number, the logger name plus a clear message almost always tells us where the call came from.

4Why Multi-Line Stack Traces Are a Problem

%ex prints the stack trace as many lines, one per frame. On a terminal that is fine. But most log shippers read files line by line, so each frame of a 60-line stack trace can arrive in the central store as a separate event, with no timestamp and no traceId. Searching for the error then shows the first line only, and the cause is scattered across dozens of meaningless entries.

Shippers can be configured with multi-line rules, such as 'a line that does not start with a date belongs to the previous event', but those rules are fragile and differ for each format. The cleaner fix is a JSON layout, where the whole event, stack trace included, is one JSON object on one line, and the stack trace sits in its own field. With Spring Boot 3.4 and later this can be as small as setting logging.structured.format.console=ecs; with older versions the logstash-logback-encoder library does the same job.

5Follow-up questions

Why is %caller or %L especially slow with async appenders?

Caller data can only be captured on the thread that made the log call, because that is the only place the call stack exists. An AsyncAppender hands the event to a background thread, so if the pattern needs caller data, the appender must capture the stack trace on the calling thread before queueing the event, which is exactly the cost we were trying to move off the request thread. That is why Logback's AsyncAppender has includeCallerData set to false by default; with it false, those words print a question mark instead of the class and line. Log4j2 async loggers work the same way, with includeLocation off by default.

Does %d in the pattern cost a lot because it formats a date every time?

Not much. Logback's date converter caches the text for the current second and only rebuilds the millisecond part, so formatting a timestamp is cheap. It is still worth using a fixed ISO format rather than a localised one, because log platforms parse ISO dates without custom rules.

What happens if I leave %ex out of the pattern?

Logback notices that the pattern has no exception conversion word and appends one automatically, so stack traces are still printed. Writing %ex explicitly, or %ex{short} or %rEx for root-cause-first output, makes the behaviour visible and lets us control the format.

6Conclusion

In this answer, we took the pattern apart: %d prints an ISO-8601 timestamp with milliseconds and offset, %-5level the level left-justified in five characters, %thread the thread name, %logger{36} the abbreviated logger name, %X{traceId} the MDC value, %msg the formatted message, %n the line separator and %ex the stack trace. We saw that caller-data words such as %class, %method, %line and %file are the expensive ones because they capture a stack trace for every event, and that multi-line stack traces break line-based shipping, which JSON layouts avoid.

What to say in the interviewAbout 80 seconds aloud

Open with

Going left to right, %d with that format prints an ISO-8601 timestamp with milliseconds and the UTC offset, like 2026-11-05T14:03:27.481+05:30. %-5level prints the level left-justified in five characters so messages line up. %thread is the thread name, for example the Tomcat worker. %logger{36} is the logger name, normally the class, abbreviated to about 36 characters by shortening the leading package names to their first letter while keeping the class name. %X{traceId} reads the traceId key from the MDC, so every line in a request carries the trace id. %msg is the message with the placeholders filled in, %n is the platform line separator, and %ex prints the exception's stack trace if one was passed.

Then, if they want more

In this pattern nothing is really expensive: the date and logger abbreviation are cached. The expensive conversion words are the caller-data ones, %class, %method, %line, %file and %caller, because Logback has to create a Throwable and walk the stack for every event, and with an async appender that has to happen on the calling thread anyway.

The other issue I would raise is that %ex prints many lines, which line-based shippers split into separate events, so for production I would prefer a JSON layout where the stack trace is a single field.

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 official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
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 "Explain this Logback pattern and what each part prints: %d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX}...": https://scrivio-interview-library.netlify.app/java/java-logging/13fc61f61f/