Given the Java argument evaluation mechanism, how does Log4j implement lazy evaluation when formatting the message with curly brackets "to avoid the cost of parameter construction" when log is disabled?
e.g.
logger.debug("Entry number: {} is {}", i, entry[i]);
I guess what Log4j means, is that with the curly brackets, they avoid constructing a String when its not necessary (e.g. the Level is not Debug):
With
logger.debug("Entry number: " + i + " is " + String.valueOf(entry[i]));
The String is always calculated even when its not logged.
with
logger.debug("Entry number: {} is {}", i, entry[i]);
Log4j can check the Log-Level first and then decide if its worth to construct the Message-String.
Log4j uses a internal Class (org.slf4j.helpers.MessageFormatter
) that replaces every {}
with the arguments provided. You can have a look at the Sourcecode in org.slf4j.impl.Log4jLoggerAdapter.debug(String, Object[])