Video in progress

Can DEBUG logs slow down the application in hot code even when they are off in prod?

Yes, if computing the arguments is expensive: a placeholder defers building the string, but the argument expressions are still evaluated. In that case you need an isDebugEnabled() check or the lazy API in SLF4J 2.

📖 Answer

// toJson() runs on every call, even when DEBUG is off
log.debug("Javob: {}", objectMapper.writeValueAsString(response));

If this method is called 5000 times a second, JSON serialization eats a noticeable part of the CPU and creates many short-lived objects, so the GC gets busy too. In a profiler this often turns out to be an "unexpected" hotspot.

The options:

if (log.isDebugEnabled()) {
    log.debug("Javob: {}", objectMapper.writeValueAsString(response));
}

// SLF4J 2.x fluent API: the supplier is called only if the level is enabled
log.atDebug()
   .setMessage("Javob: {}")
   .addArgument(() -> toJson(response))
   .log();

In simple cases (log.debug("id={}", id)) the check isn't needed: the placeholder alone is enough, and an extra if only clutters the code.

Another thing: the cost of a log isn't just formatting. If you write 10 lines per request at INFO level, writing to disk, JSON encoding and sending over the network all add load too. Sometimes the fastest optimization is deleting an unneeded log line.