Skip to content

Add StringFormat, MessageFormat, and RemoveUnnecessaryLogLevelGuards SLF4J recipes.#280

Merged
timtebeek merged 6 commits intoopenrewrite:mainfrom
motlin:add-slf4j-logging-recipes
Feb 26, 2026
Merged

Add StringFormat, MessageFormat, and RemoveUnnecessaryLogLevelGuards SLF4J recipes.#280
timtebeek merged 6 commits intoopenrewrite:mainfrom
motlin:add-slf4j-logging-recipes

Conversation

@motlin
Copy link
Contributor

@motlin motlin commented Feb 25, 2026

What's changed?

Add three new SLF4J recipes to Slf4jBestPractices:

  • StringFormatToParameterizedLogging — Replaces String.format() calls in SLF4J logging statements with parameterized {} placeholders.
  • MessageFormatToParameterizedLogging — Replaces MessageFormat.format() calls in SLF4J logging statements with parameterized {} placeholders.
  • RemoveUnnecessaryLogLevelGuards — Removes if (logger.isDebugEnabled()) guards around SLF4J logging calls when parameterized logging makes them unnecessary.

The StringFormat and MessageFormat recipes are ordered before the existing ParameterizedLogging recipe in slf4j.yml so that format calls are converted to parameterized logging first, before ParameterizedLogging handles string concatenation.

What's your motivation?

String.format() and MessageFormat.format() defeat the purpose of SLF4J's parameterized logging by eagerly formatting the message regardless of log level. The existing ParameterizedLogging recipe handles string concatenation but not these cases.

Similarly, once logging statements use parameterized {} placeholders, explicit isDebugEnabled()/isTraceEnabled() guards become unnecessary boilerplate since SLF4J defers argument evaluation.

Anything in particular you'd like reviewers to focus on?

  • The RemoveUnnecessaryLogLevelGuards recipe only removes guards when all logging calls inside the guard body use parameterized logging (no string concatenation or method calls other than toString()).

Anyone you would like to review specifically?

@timtebeek saw these recipes as I was developing them in Liftwizard:

  • liftwizard#3336 — Add StringFormatLoggingToParameterized, MessageFormatLoggingToParameterized, and RemoveUnnecessaryLogLevelGuards recipes
  • liftwizard#3343 — Rename logging recipes to ToParameterizedLogging naming convention
  • liftwizard#3484 — Handle concatenated string literal format arguments in logging recipes

Have you considered any alternatives or workarounds?

I considered splitting this into two PRs and I could still do that if preferred.

  • One for the two formatted strings to parameterized logging recipes.
  • One for unnecessary log level guards.

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

RemoveUnnecessaryLogLevelGuards incorrectly removes guards around
logging calls that use string concatenation (e.g. `"Name: " + name`)
instead of parameterized `{}` placeholders. The guard is necessary
in these cases because concatenation is evaluated eagerly.

Validated at scale against the Default org where Netflix/ribbon had
two false positives in DiscoveryEnabledNIWSServerList and RestClient.
Copy link
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice additions, thanks! I've added some polish mostly to use ListUtils and other ways to minimize the changeset.

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Feb 26, 2026
@timtebeek timtebeek merged commit 2f457be into openrewrite:main Feb 26, 2026
1 check passed
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Feb 26, 2026
@timtebeek timtebeek added the recipe Recipe Requested label Feb 26, 2026
@motlin motlin deleted the add-slf4j-logging-recipes branch February 26, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

recipe Recipe Requested

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants