lazy

public static LazyArg<T> lazy<T>(LazyArg<T> lambdaOrMethodReference)

Coerces a lambda expression or method reference to return a lazily evaluated logging argument. Pass in a compatible, no-argument, lambda expression or method reference to have it evaluated only when logging will actually occur.


logger.atFine().log("value=%s", lazy(() -> doExpensive()));
logger.atWarning().atMostEvery(5, MINUTES).log("value=%s", lazy(stats::create));
Evaluation of lazy arguments occurs at most once, and always in the same thread from which the logging call was made.

Note also that it is almost never suitable to make a toString() call "lazy" using this mechanism and, in general, explicitly calling toString() on arguments which are being logged is an error as it precludes the ability to log an argument structurally.