You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes parameters to logging invocations include expensive computations which are pointless if the log level isn't set such that the message will actually be recorded.
It would be useful to have a recipe to remediate this. In slf4j 1 or 2 you can wrap the expensive logging statement in a conditional like so:
if (logger.isDebugEnabled()) {
logger.debug("Something important {}", objectMapper.writeValueToString(object));
}
In slf4j 2 there is the option of wrapping the expensive parameter in a lambda:
logger.debug("Something important {}", () -> objectMapper.writeValueAsString(object));
Note that in this case checks would have to be made that all identifiers in lambda were final or effectively final, per Java's rules about what data may be included in a closure.
I believe there is an RSPEC number for this but I do not know what it is.
The text was updated successfully, but these errors were encountered:
sambsnyd
changed the title
Recipe to put expensive logger parameters into lambdas
Recipe to put expensive logger parameters into lambdas/conditionals
Nov 15, 2022
Sometimes parameters to logging invocations include expensive computations which are pointless if the log level isn't set such that the message will actually be recorded.
It would be useful to have a recipe to remediate this. In slf4j 1 or 2 you can wrap the expensive logging statement in a conditional like so:
In slf4j 2 there is the option of wrapping the expensive parameter in a lambda:
Note that in this case checks would have to be made that all identifiers in lambda were final or effectively final, per Java's rules about what data may be included in a closure.
I believe there is an RSPEC number for this but I do not know what it is.
The text was updated successfully, but these errors were encountered: