Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Which of these is better and why: log.debug("Order " + order + " total " + total()); or log.debug("Order {} total {}", order, total());?

The parameterised form, log.debug("Order {} total {}", order, total()), is better, because the logger builds the message only if DEBUG is enabled, while the concatenated form builds the full string on every call even when the line is thrown away. But both forms still evaluate their arguments, so total() runs in both. When an argument is expensive, we guard it with if (log.isDebugEnabled()) or pass a Supplier through the SLF4J 2 fluent API, and we take care that toString() of large objects is neither slow nor leaking sensitive data.

Why this question is asked

Debug statements sit in the hottest code paths of most services, and in production DEBUG is almost always off. So the question is really about what a disabled log line still costs us, multiplied by thousands of calls per second. Interviewers like this small question because the common answer, "the second is faster", is only half right: there is a cost that the placeholders do not remove. In this answer, we will see what each form does step by step, measure where the waste is, look at the cost that remains in both, fix it with a guard or a lazy Supplier, and finish with the toString() trap.

1What the Concatenated Form Does

Java evaluates every argument of a method call before the method runs. So for the first form, the string is built before log.debug() is even entered, and the logger only then gets the chance to say it does not want it.

Java
// DEBUG is off in production
log.debug("Order " + order + " total " + total());
// 1. total() runs -> 129.90
// 2. order.toString() runs -> "Order[id=42, lines=[...]]"
// 3. a StringBuilder joins four pieces -> new String of about 200 chars
// 4. log.debug(...) checks the level -> disabled, string thrown away

Steps 2 and 3 are pure waste: we called toString() on the order, allocated a builder and a new String, and immediately discarded them. In a loop that runs 50,000 times a second, that is 50,000 garbage strings per second for a line nobody reads, which means extra allocation and extra work for the garbage collector.

2What the Parameterised Form Changes

With placeholders, we pass a constant format string and the raw arguments. SLF4J checks the level first, and only if the level is enabled does it replace each {} with String.valueOf() of the matching argument.

Java
log.debug("Order {} total {}", order, total());
// 1. total() runs -> 129.90 (arguments are still evaluated)
// 2. log.debug(...) checks the level -> disabled, returns immediately
// order.toString() never runs, no message string is built
What a disabled DEBUG line costs
WorkConcatenationPlaceholders
Evaluate total()yesyes
Call order.toString()yesno
Build the message stringyesno
Box arguments, varargs arraynosometimes (3+ args use an array)
Check the levelyesyes
Placeholders remove the formatting, but the arguments are still computed.

So the formatting cost, which includes the toString() call on the order, is paid only when the line is actually written. The format string is a constant in the class file, so it costs nothing to pass. Notice, though, that the order object and the result of total() are still passed, and with two or more primitive arguments Java may box them or create a varargs array before the call. Those costs are small compared to building the message, but they are not zero.

3The Cost That Remains: Arguments Are Still Evaluated

This is the part many people miss. Placeholders delay formatting, not evaluation. If total() walks 500 order lines and applies tax rules, it runs on every call in both versions, and the result is thrown away when DEBUG is off. The same is true for anything we compute inline, such as list.stream().map(...).toList() or a JSON serialisation of a payload.

There is also a subtle correctness point. If total() has a side effect, for example it caches a value or increments a counter, then turning DEBUG on or off does not change whether it runs. That is good in this case. But it also means that a slow argument stays slow in production even though we cannot see the log line it feeds, which makes it a hard performance problem to spot.

4Fixing Expensive Arguments

For a cheap argument, placeholders are enough. For an expensive one, we have two good options. The classic one is a level guard. The modern one is the fluent API that SLF4J added in version 2.0, which accepts a Supplier, a lambda the logger calls only when the level is enabled.

Java
// Option 1: guard the whole statement
if (log.isDebugEnabled()) {
log.debug("Order {} total {}", order.id(), total()); // total() runs only when DEBUG is on
}
// Option 2: SLF4J 2 fluent API with a lazy argument
log.atDebug()
.setMessage("Order {} total {}")
.addArgument(order.id())
.addArgument(() -> total()) // Supplier: called only if DEBUG is enabled
.log();

The guard is explicit and works with every SLF4J version, and it is the right choice when several expensive values are prepared for one line. The fluent API keeps the statement on one expression and is handy when only one argument is expensive. Spring Boot 3 and later ship SLF4J 2, so the fluent API is available by default. Log4j2's own API offers the same idea with Supplier parameters, if a project uses it directly.

Notice that the guard costs one level check, which is a cheap comparison, so wrapping cheap statements in it only adds noise. We use it where the profiler or common sense says an argument is expensive.

5The toString() Trap

The last point is about what we pass rather than how. Passing a whole object means its toString() decides what we write. For a large object, such as an order with 2,000 lines or an entity with lazy collections, toString() can be slow, can produce a 100 KB log line, and with JPA entities can even trigger lazy loading or a LazyInitializationException. For an object that holds personal data, toString() may print an email or address into the log, which breaks privacy rules.

So the better line in the question is actually log.debug("Order {} total {}", order.id(), total()), with the id instead of the object. It is cheaper, it is short enough to read, and it cannot leak more than we intended. If we really need more detail at DEBUG, we log the few fields we care about by name.

6Follow-up questions

Does the JIT not remove the string building when DEBUG is off?

It usually cannot. The level is a runtime value that can change while the application runs, and toString() and total() may have side effects, so the JIT must keep the calls. Escape analysis may avoid some allocations, but we should not rely on it. The parameterised form removes the work by design, not by optimisation.

Is it worth adding isDebugEnabled() around every debug line?

No. With placeholders and cheap arguments the logger already does that check first, so an extra guard only adds lines and nesting. We add a guard, or use a Supplier, only when computing an argument is expensive, such as a stream over a large collection, a serialisation or a remote call.

What happens if the number of placeholders and arguments do not match?

SLF4J leaves extra placeholders as {} and ignores surplus arguments, except that a Throwable as the last extra argument is treated as the exception and its stack trace is printed. So log.error("failed for {}", id, ex) prints the id and the stack trace, which is the usual way to log an exception with a message.

7Conclusion

In this answer, we saw that string concatenation builds the whole message, including toString() of every object, before the logger can check the level, while the parameterised form formats only when DEBUG is enabled. We also saw that both forms still evaluate their arguments, so an expensive call such as total() runs either way, and we fixed that with an isDebugEnabled() guard or a Supplier in the SLF4J 2 fluent API. Finally, we noted that passing whole objects makes toString() a performance and privacy risk, so logging the id is usually the best choice.

What to say in the interviewAbout 80 seconds aloud

Open with

The parameterised version is better. With concatenation, Java builds the whole string before log.debug is called: it calls order.toString, builds a new String with a StringBuilder, and only then does the logger check the level and throw it away.

Then, if they want more

In production, where DEBUG is off, that is pure garbage on every call. With placeholders, SLF4J checks the level first and only formats the message, including the toString call, if DEBUG is actually enabled. But I would point out that placeholders delay formatting, not evaluation. Both forms still call total(), because Java evaluates arguments before the method runs.

So if total() is expensive, I either wrap the statement in if log.isDebugEnabled, or I use the SLF4J 2 fluent API, log.atDebug().setMessage(...).addArgument with a Supplier, so total() is only called when the line is really written.

And I would also change what is passed: logging the whole order means its toString decides the output, which can be slow for a big object, can trigger lazy loading on an entity, and can leak personal data. So my preferred line is log.debug with placeholders, order.id() and the total, guarded only if total is expensive.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3
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 "Which of these is better and why: log.debug("Order " + order + " total " + total()); or...": https://scrivio-interview-library.netlify.app/spring-boot/boot-logging/f7426ca83b/