Skip to content

Remove loglevel guards when using parameterized logging  #127

@timtebeek

Description

@timtebeek

What problem are you trying to solve?

Loglevel guards ( such as if (LOG.isDebugEnabled())) are a performance improvement to prevent building up logging statement arguments when the log line would be discarded. Parameterized logging is a similar approach at limiting the impact of logging on performance, by only formatting the logging statement when the loglevel is enabled. Code should likely only have to use one of the two approaches: either loglevel guards, or parameterized logging. So when paramterized logging statements are used, the loglevel guards can be removed.

What precondition(s) should be checked before applying this recipe?

The loglevel guard should only be removed if

  • all statements in the body are logging statements
  • all logging statements have arguments that are not method invocations (maybe allow Exception.getMessage()), to prevent side effects

Describe the situation before applying the recipe

if (LOG.isDebugEnabled()) {
    LOG.debug("Swallowed an IOException caused by client connectivity: {}", cause.getMessage(), cause);
}

Describe the situation after applying the recipe

-if (LOG.isDebugEnabled()) {
-    LOG.debug("Swallowed an IOException caused by client connectivity: {}", cause.getMessage(), cause);
+LOG.debug("Swallowed an IOException caused by client connectivity: {}", cause.getMessage(), cause);
-}

Any additional context

As discovered working through

Metadata

Metadata

Assignees

No one assigned

    Labels

    recipeRecipe Requested

    Type

    No type

    Projects

    Status

    Recipes Wanted

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions