I have a debug log statement in my code to which I am passing a result of costly method call.
log.debug("Data - {}", obj.costlyMethodCall());
Now, even if debug logging is disabled, obj.costlyMethodCall() will always be evaluated to be passed as method argument.
One way to mitigate this is to have a explicit check if debug logging is enabled
if (log.isDebugEnabled()) {
log.debug("Data - {}", obj.costlyMethodCall());
}
But this reduces code readability.
Is there any better way to make invocation of obj.costlyMethodCall() lazy or conditional here?