Where is checking to see if the requested log level is enabled?
Inside the logger.trace()
method.
The trick here, however, is how you pass the argument. The pre-java8 style computed value during a call to logger.trace
.
logger.trace(..., expensiveOperation());
Java 8 style uses Supplier
logger.trace( ..., () -> expensiveOperation());
Thus, expensiveOperation()
is called only on request - inside the trace
method.
Take a look at the java.util.logging.Logger.log()
implementation:
public void log(Level level, Supplier<String> msgSupplier) { if (!isLoggable(level)) { return; } LogRecord lr = new LogRecord(level, msgSupplier.get());
source share