Skip to content

Add Log4j2IsEnabledToSlf4j Refaster recipe to convert isEnabled(Level) level guards to SLF4J isXEnabled() calls.#294

Merged
timtebeek merged 2 commits into
openrewrite:mainfrom
motlin:log4j2-isenabled-to-slf4j
May 28, 2026
Merged

Add Log4j2IsEnabledToSlf4j Refaster recipe to convert isEnabled(Level) level guards to SLF4J isXEnabled() calls.#294
timtebeek merged 2 commits into
openrewrite:mainfrom
motlin:log4j2-isenabled-to-slf4j

Conversation

@motlin

@motlin motlin commented May 28, 2026

Copy link
Copy Markdown
Contributor

What's changed?

Adds a new Refaster recipe Log4j2IsEnabledToSlf4j that converts Log4j 2.x Logger.isEnabled(Level.X) level guards to the corresponding SLF4J isXEnabled() calls:

Log4j 2.x SLF4J
isEnabled(Level.TRACE) / isEnabled(Level.ALL) isTraceEnabled()
isEnabled(Level.DEBUG) isDebugEnabled()
isEnabled(Level.INFO) isInfoEnabled()
isEnabled(Level.WARN) isWarnEnabled()
isEnabled(Level.ERROR) / isEnabled(Level.FATAL) isErrorEnabled()

It's wired into Log4j2ToSlf4j1 (in slf4j.yml) ahead of the ChangeType step that retypes org.apache.logging.log4j.Logger to org.slf4j.Logger, so the Refaster templates still match while the Log4j type is present.

What's your motivation?

The Log4j to SLF4J migration retyped the logger but left isEnabled(Level.X) guards untouched, leaving code that no longer compiles - SLF4J's Logger has no isEnabled(Level) method.

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

Why isXEnabled() rather than SLF4J 2.x's isEnabledForLevel(Level)?
SLF4J 2.0 added boolean isEnabledForLevel(org.slf4j.event.Level), which would preserve the parameterized form. I deliberately did not use it because:

  1. Version target. This recipe is Migrate Log4j 2.x to SLF4J 1.x (and is reused by Log4j1ToSlf4j1). isEnabledForLevel does not exist in SLF4J 1.7, so emitting it would produce code that doesn't compile for the 1.x users this recipe targets. isXEnabled() compiles on both 1.x and 2.x.
  2. No ALL/FATAL constants. org.slf4j.event.Level only defines TRACE, DEBUG, INFO, WARN, ERROR. Log4j's ALL and FATAL have no SLF4J enum equivalent, so they'd require a fallback mapping regardless of which method is used.

Lossy level mappings: ALL to isTraceEnabled() and FATALto isErrorEnabled(), since SLF4J has no ALL or FATAL level. These collapse into the existing recipes via a second @BeforeTemplate per class - flagging in case a different convention is preferred.

Recipe ordering in slf4j.yml: must run before the Logger ChangeType.

@motlin motlin force-pushed the log4j2-isenabled-to-slf4j branch from d36dd61 to 20fb8f2 Compare May 28, 2026 21:49

@timtebeek timtebeek left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perfect addition, thanks! Should help code to conform to standards, and recipes to chain more easily.

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite May 28, 2026
@timtebeek timtebeek merged commit 944b9c2 into openrewrite:main May 28, 2026
1 check passed
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite May 28, 2026
@motlin motlin deleted the log4j2-isenabled-to-slf4j branch May 29, 2026 02:34
mergify Bot added a commit to robfrank/linklift that referenced this pull request Jun 4, 2026
…rom 3.27.2 to 3.29.0 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-logging-frameworks](https://github.com/openrewrite/rewrite-logging-frameworks) from 3.27.2 to 3.29.0.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-logging-frameworks's releases](https://github.com/openrewrite/rewrite-logging-frameworks/releases).*

> 3.29.0
> ------
>
> What's Changed
> --------------
>
> * Regenerate recipes.csv by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-logging-frameworks#292](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/pull/292)
> * Fix re-escaping of control characters when rebuilding Java string literals in logging recipes. by [`@​motlin`](https://github.com/motlin) in [openrewrite/rewrite-logging-frameworks#293](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/pull/293)
> * Add Log4j2IsEnabledToSlf4j Refaster recipe to convert isEnabled(Level) level guards to SLF4J isXEnabled() calls. by [`@​motlin`](https://github.com/motlin) in [openrewrite/rewrite-logging-frameworks#294](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/pull/294)
>
> **Full Changelog**: <openrewrite/rewrite-logging-frameworks@v3.28.0...v3.29.0>
>
> 3.28.0
> ------
>
> What's Changed
> --------------
>
> * Renew log4j 1.2.17 reference suppression to 2026-06-01 by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-logging-frameworks#287](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/pull/287)
> * Update log4j-api TypeTable to 2.26.0 by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-logging-frameworks#288](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/pull/288)
> * Preserve formatting when ParameterizedLogging has nothing to parameterize by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-logging-frameworks#290](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/pull/290)
>
> **Full Changelog**: <openrewrite/rewrite-logging-frameworks@v3.27.0...v3.28.0>
>
> v3.27.3
> -------
>
> What's Changed
> --------------
>
> * OpenRewrite [v8.81.6](https://github.com/openrewrite/rewrite/releases/tag/v8.81.6)
> * Fix IndexOutOfBoundsException when JUL placeholders exceed argument count by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-logging-frameworks#284](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/pull/284)
> * Fix exception consumed by format placeholder in CompleteExceptionLogging by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-logging-frameworks#285](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/pull/285)
>
> **Full Changelog**: <openrewrite/rewrite-logging-frameworks@v3.26.0...v3.27.3>


Commits

* [`944b9c2`](openrewrite/rewrite-logging-frameworks@944b9c2) Add Log4j2IsEnabledToSlf4j Refaster recipe to convert isEnabled(Level) level ...
* [`246f4d3`](openrewrite/rewrite-logging-frameworks@246f4d3) Fix re-escaping of control characters when rebuilding Java string literals in...
* [`d5537a4`](openrewrite/rewrite-logging-frameworks@d5537a4) Update Gradle wrapper 9.5.1
* [`58e431b`](openrewrite/rewrite-logging-frameworks@58e431b) Regenerate recipes.csv ([#292](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/issues/292))
* [`28afd35`](openrewrite/rewrite-logging-frameworks@28afd35) Remove explicit test dependencies
* [`14f50ad`](openrewrite/rewrite-logging-frameworks@14f50ad) Preserve formatting when ParameterizedLogging has nothing to parameterize ([#2](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/issues/2)...
* [`32e22cb`](openrewrite/rewrite-logging-frameworks@32e22cb) Update log4j-api TypeTable to 2.26.0 ([#288](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/issues/288))
* [`b7824c6`](openrewrite/rewrite-logging-frameworks@b7824c6) OpenRewrite recipe best practices
* [`43fc6c2`](openrewrite/rewrite-logging-frameworks@43fc6c2) Renew log4j 1.2.17 reference suppression to 2026-06-01 ([#287](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/issues/287))
* See full diff in [compare view](openrewrite/rewrite-logging-frameworks@v3.27.2...v3.29.0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants