Quick revision
The short answer
The first call concatenates strings, so the message is built even when DEBUG is off; it should be log.debug("Loaded {} items: {}", items.size(), items). The second call logs only e.getMessage(), which loses the stack trace and the cause chain; we pass the exception itself as the last argument, as in log.error("Failed to process order {}", orderId, e), and SLF4J prints its full stack trace. The third call has two placeholders and only one argument, so the second {} is printed literally; every placeholder needs an argument, and for arguments that are expensive to compute we use SLF4J 2's fluent API with a supplier or an isDebugEnabled() guard.
What to remember
In this answer, we fixed three common SLF4J mistakes. Concatenation builds the message even when the level is disabled, so we replaced it with placeholders. Logging e.getMessage() throws away the stack trace and cause chain, so we passed the exception as the last argument, where SLF4J attaches it to the event. A placeholder without an argument is printed literally, so we matched the counts and suggested a static analysis rule. Finally, for expensive arguments we used SLF4J 2's fluent API with a supplier or an isDebugEnabled() guard.
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.
The first call concatenates strings, so the message is built even when DEBUG is off; it should be log.debug("Loaded {} items: {}", items.size(), items). The second call logs only e.getMessage(), which loses the stack trace and the cause chain; we pass the exception itself as the last argument, as in log.error("Failed to process order {}", orderId, e), and SLF4J prints its full stack trace. The third call has two placeholders and only one argument, so the second {} is printed literally; every placeholder needs an argument, and for arguments that are expensive to compute we use SLF4J 2's fluent API with a supplier or an isDebugEnabled() guard.
Why this question is asked
Log statements are written quickly and reviewed rarely, so small mistakes in them survive for years. These three mistakes are the most common ones in Java code bases, and each has a real cost: wasted CPU and garbage on hot paths, incidents debugged without a stack trace, and log lines that do not say what happened. Interviewers use this exercise to see whether we know how SLF4J treats its arguments. In this answer, we will recap how parameterized messages work, fix each call in turn with the output before and after, and then look at the fluent API and when a guard is still worth writing.
1A Quick Recap of Parameterized Messages
SLF4J methods take a message pattern and arguments. Each {} in the pattern is a placeholder, and SLF4J replaces placeholders with the arguments in order, using each argument's toString(). The replacement happens only if the event is actually going to be written, after the level check passes, so a disabled call does almost no work.
There is one special rule: if the last argument is a Throwable and there is no placeholder left for it, SLF4J treats it as the event's exception and prints its stack trace, rather than formatting it into the message. These two rules are enough to fix all three calls.
2Fix 1: Replace Concatenation With Placeholders
In Java, arguments are evaluated before the method is called. So in the original first call, the + operators build a new string, including items.toString() of the whole list, before debug() even gets to check whether DEBUG is enabled. In production, where DEBUG is usually off, that string is built and thrown away on every call.
Notice that items.size() is still called in the fixed version, because it is an argument, but that is a cheap method returning an int. What we saved is building the message and calling items.toString(), which for a long list could produce kilobytes of text.
3Fix 2: Pass the Exception, Not Its Message
The second call keeps only the exception's message, which is often short and vague, such as 'Connection reset' or even null. The stack trace, which tells us where the failure happened, and the cause chain, which tells us why, are lost. Hours of debugging often come down to this one mistake.
There is one placeholder and two arguments. SLF4J fills {} with orderId, sees that the remaining last argument is a Throwable, and attaches it to the event, so the layout prints the full trace including every Caused by. We also improved the message itself: it says which order failed, which the original never did. The exception must be the last argument, and it must not have its own {}; if we wrote a placeholder for it, only its toString() would appear in the message.
4Fix 3: Match Placeholders to Arguments
The third call has two placeholders but passes only userId. SLF4J does not throw or warn; it fills the first placeholder and leaves the second one as literal text.
| Original call | What goes wrong | Fixed call |
|---|---|---|
"Loaded " + items.size() + ... | string built even when DEBUG is off | "Loaded {} items: {}", items.size(), items |
"Failed: " + e.getMessage() | no stack trace, no cause chain | "Failed to process order {}", orderId, e |
"User {} logged in from {}", userId | second {} printed literally | "User {} logged in from {}", userId, clientIp |
Because nothing fails at runtime, this kind of mistake is best caught by tools. Static analysis such as Error Prone or SonarQube has rules that compare the number of placeholders with the number of arguments, and IntelliJ IDEA flags it as an inspection. Adding such a rule to the build catches these once and for all.
5Expensive Arguments: Fluent API and Guards
Placeholders delay the formatting, but not the evaluation of the arguments. If an argument is itself expensive, for example a method that serializes an object graph to JSON, it runs on every call even with DEBUG off. SLF4J 2 added a fluent API that accepts suppliers, which are called only if the event is enabled. The older alternative is to guard the call with isDebugEnabled().
6Follow-up questions
When is an isDebugEnabled() guard still worthwhile?
When the work to prepare the log arguments is expensive, such as serializing an object, running a query for diagnostic data or looping to build a summary, and we are not on SLF4J 2 or prefer the plain style. It is also worth it when several log statements share the same expensive preparation. For ordinary arguments such as ids and sizes, the guard only adds noise, because the parameterized call already checks the level first.
Is passing more than two arguments slower?
Slightly. SLF4J has overloads for one and two arguments; three or more use varargs, which creates an Object array on every call, even when the level is disabled. On a very hot path, that allocation can matter, and a guard or the fluent API avoids it; elsewhere, it is negligible.
Should we log and rethrow the exception?
Usually not. If we log an exception and rethrow it, the code that finally handles it will often log it again, and the same failure appears several times in the logs. The rule of thumb is to either handle the exception and log it there, or rethrow it with context and let the top-level handler log it once.
7Conclusion
In this answer, we fixed three common SLF4J mistakes. Concatenation builds the message even when the level is disabled, so we replaced it with placeholders. Logging e.getMessage() throws away the stack trace and cause chain, so we passed the exception as the last argument, where SLF4J attaches it to the event. A placeholder without an argument is printed literally, so we matched the counts and suggested a static analysis rule. Finally, for expensive arguments we used SLF4J 2's fluent API with a supplier or an isDebugEnabled() guard.
Open with
Each of the three calls has a different problem. The first one uses string concatenation. Java evaluates arguments before calling the method, so the whole string, including the toString of every item, is built even when DEBUG is off, and then thrown away.
Then, if they want more
I would write log.debug with Loaded curly braces items colon curly braces, and pass items.size() and items as arguments, so with DEBUG off the call returns after the level check without building anything. The second call logs only e.getMessage(), which loses the stack trace and the cause chain, the most useful parts when debugging.
I would write log.error with Failed to process order curly braces, then the order id, then the exception itself as the last argument. SLF4J sees that the last argument is a Throwable without a placeholder and prints the full stack trace, including Caused by lines.
The third call has two placeholders but one argument, and SLF4J does not complain; it just prints the second curly braces literally, so the line says logged in from curly braces. The fix is to pass the client IP as the second argument, and I would add a static analysis rule that checks placeholder counts.
One more point: placeholders delay formatting but not the evaluation of arguments, so for an expensive argument like serializing an object, I would use SLF4J 2's fluent API, atDebug with addArgument and a supplier, or wrap the call in an isDebugEnabled check.
About this answer
- Last updated
- 12 November 2026
- 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 "Rewrite these logging calls correctly with the SLF4J API and explain what is wrong with...": https://scrivio-interview-library.netlify.app/java/java-logging/c0e50f7e4c/