Quick revision
The short answer
Do not build messages eagerly with +. An eager concatenation allocates and formats the message even when the log level is disabled, which in a logging-heavy library is pure waste on every call. Pass a message pattern with arguments, such as SLF4J's "{}" placeholders, or a Supplier, so formatting happens only when the message will be written; guard expensive argument computation with a level check or a supplier; avoid String.format in hot paths because it parses the pattern on every call; and build the preferred pattern into the library's own API so callers get it by default.
What to remember
In this answer, we saw that an eager concatenated log message is built before the logger checks its level, so disabled debug lines still allocate and call toString(). Parameterized logging with {} placeholders defers formatting until the event is enabled, suppliers and level guards defer expensive argument computation, and String.format is the costliest choice because it parses its pattern on every call. The most effective guidance is to make these patterns the library's default API, log through a facade, and back the rule with static analysis.
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.
Do not build messages eagerly with +. An eager concatenation allocates and formats the message even when the log level is disabled, which in a logging-heavy library is pure waste on every call. Pass a message pattern with arguments, such as SLF4J's "{}" placeholders, or a Supplier, so formatting happens only when the message will be written; guard expensive argument computation with a level check or a supplier; avoid String.format in hot paths because it parses the pattern on every call; and build the preferred pattern into the library's own API so callers get it by default.
Why this question is asked
Logging is the one piece of code that runs on almost every request, and most of its output is thrown away because debug and trace levels are off in production. That makes the cost of a disabled log statement surprisingly important. Interviewers ask this design question to see whether we can reason about where the cost actually goes and turn that into guidance a whole team, and its users, will follow. In this answer, we will measure what an eager message costs when the level is off, compare parameterized logging and suppliers, look at guarding expensive arguments, explain why String.format is the worst choice in hot paths, and finish with how to shape the library's API.
1A Quick Recap: What a Disabled Log Call Costs
When we write log.debug("order " + id + " for " + user), Java evaluates the argument before calling debug. That is ordinary method-call semantics: arguments are computed first. So the concatenation runs, calls user.toString(), allocates a new String, and only then does the logger check whether debug is enabled and throw the message away.
At debug level off, that is a wasted String allocation and a toString() call on every invocation. In a library that logs inside tight loops, such as a parser or a client that logs each request, the waste can show up as a noticeable share of allocation rate and CPU.
With the parameterized form, the logger receives the pattern and the argument references. It checks the level first and formats only if the message will be written. When the level is off, the cost drops to a level check and, for a primitive like a long orderId, possibly an autoboxing.
2Parameterized Logging and Suppliers
SLF4J and Log4j 2 both support placeholder patterns. The logger substitutes each {} with the matching argument's string form only when the event is enabled. For one or two arguments, SLF4J has fixed-arity methods, so there is no array allocation; three or more arguments use varargs, which allocates an Object[] for each call even when disabled. Log4j 2 offers more fixed-arity overloads.
Suppliers go one step further: the whole message, or an argument, is computed by a lambda that runs only if the level is enabled. This helps when building an argument is itself expensive, not just formatting it.
Notice the trade-off. A lambda that captures variables may allocate a small object per call, unless the JIT removes it, so for cheap arguments the plain placeholder form is the better default. Suppliers are for expensive work.
3Guarding Expensive Work
Sometimes the expensive part is not formatting but computing the data, for example summarising a large collection or calling a method that does I/O. Placeholders do not help there, because the argument is still evaluated before the call. A level check or a supplier is the way to avoid it.
- Arguments evaluatedalways, by Java's call rules
- Level enabled?YesFormat the messageNoReturn: cost is the arguments only
- Format pattern with arguments
toString()runs only now - Write to appenders
4 steps in order. Play them, or step through them one at a time.
Anything computed in the arguments is paid for even when the level is off.
Level checks read a little noisier, so we use them only where the argument is expensive, not around every log line. In a library, the check also lets us skip whole blocks of diagnostic work, such as collecting timings, when nobody will see the result.
4Why String.format Is a Poor Choice in Hot Paths
String.format("order %s for %s", id, customer) looks tidy, but it is the most expensive option. Each call creates a new Formatter, parses the format string to find every % specifier, allocates a varargs array, and handles locale-sensitive conversions. That parsing is repeated on every call, because the parsed pattern is not cached. Used eagerly in a log call, it pays all of that even when the level is off.
SLF4J's {} substitution is a simple scan for braces, much cheaper than Formatter. And plain + is cheaper still when we really need a String, thanks to invokedynamic concatenation. So String.format belongs in code where its features, widths, precision and locales, are needed and the call rate is low.
5Shaping the Library's API
The best guidance is the one that does not need to be remembered. If the library wraps its logging in a small internal helper, that helper can offer only the efficient forms: methods that take a pattern and arguments, and overloads that take a Supplier. Then a contributor cannot easily write the eager form, and code review has one rule to check.
If the library exposes hooks for users' own messages, such as an event listener or a diagnostic callback, it should pass structured data or suppliers rather than prebuilt strings, so that the user's logging level decides whether the message is ever built. Finally, the library should log through a facade such as SLF4J or System.Logger rather than a concrete framework, so applications keep control of levels and output. We can back all of this with a static analysis rule that flags string concatenation inside log calls.
6Follow-up questions
Does parameterized logging still allocate anything when disabled?
Usually very little. Boxing a primitive argument creates an object for values outside the small cache, and SLF4J's varargs methods allocate an array for three or more arguments. The JIT can often remove these when the call is inlined and the level check fails, but the reliable way to reach zero is a level guard around the call.
What about structured logging with key-value pairs?
It follows the same rule. SLF4J 2's fluent API with addKeyValue, or a structured logging layout, stores values as data and serialises them only when the event is written. That is better than concatenating key=value text by hand, both for cost and for parsing in the log system.
Is eager concatenation ever acceptable?
In code that runs rarely, such as startup or error paths at WARN and ERROR levels, which are normally enabled, the difference is negligible and readability wins. The rule matters for DEBUG and TRACE in frequently called code, which is most of a logging-heavy library.
7Conclusion
In this answer, we saw that an eager concatenated log message is built before the logger checks its level, so disabled debug lines still allocate and call toString(). Parameterized logging with {} placeholders defers formatting until the event is enabled, suppliers and level guards defer expensive argument computation, and String.format is the costliest choice because it parses its pattern on every call. The most effective guidance is to make these patterns the library's default API, log through a facade, and back the rule with static analysis.
Open with
My guidance would be: never build log messages eagerly with plus in this library. Java evaluates arguments before the call, so log.debug with a concatenation builds the String and calls toString on every argument even when debug is off, which in production it almost always is.
Then, if they want more
In a logging-heavy library that is pure allocation and CPU on every call. Instead, pass a pattern with placeholders, like SLF4J's curly braces, so the logger checks the level first and formats only when the event will be written.
When an argument itself is expensive to compute, say summarising a big collection, placeholders do not help because the argument is still evaluated, so I would use a Supplier, which both Log4j 2 and the SLF4J 2 fluent API support, or an isDebugEnabled guard around that one statement.
I would also keep String.format out of hot paths, because it creates a Formatter and parses the format string on every call, so it is the most expensive option of all.
Finally, I would make the right pattern the default rather than a rule people must remember: an internal logging helper that only offers pattern-and-arguments and Supplier methods, logging through a facade so applications control levels, callbacks that pass data instead of prebuilt strings, and a static analysis check that flags concatenation inside log calls.
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 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
- The Java Language Specification, Java SE 21, Java SE 21 API 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 "A team writing a logging-heavy library asks whether to build messages eagerly with + or...": https://scrivio-interview-library.netlify.app/java/strings/0a59202682/