Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe to put expensive logger parameters into lambdas/conditionals #81

Open
sambsnyd opened this issue Nov 15, 2022 · 0 comments
Open
Labels
good first issue Good for newcomers recipe Recipe Requested

Comments

@sambsnyd
Copy link
Member

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.

@sambsnyd sambsnyd added the recipe Recipe Requested label Nov 15, 2022
@sambsnyd sambsnyd changed the title Recipe to put expensive logger parameters into lambdas Recipe to put expensive logger parameters into lambdas/conditionals Nov 15, 2022
@tkvangorder tkvangorder moved this to Recipes Wanted in OpenRewrite Nov 16, 2022
@timtebeek timtebeek added the good first issue Good for newcomers label Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers recipe Recipe Requested
Projects
Status: Recipes Wanted
Development

No branches or pull requests

2 participants