Given the Java argument evaluation mechanism, how does Log4j 2.x 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 complete message is always computed even when it will not be 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. This saves resources needed for string concatenation. It will only call toString()
on the objects supplied if the message is actually being created, saving further computation costs.
Log4j uses an internal class (org.apache.logging.log4j.message.ParameterFormatter
) that replaces every {}
with the arguments provided. It will also catch exceptions thrown by toString()
and report these failures.