GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add `slf4j-api` when migrating Log4j to SLF4J 1.x by knutwannheden · Pull Request #341 · openrewrite/rewrite-logging-frameworks

Conversation

@knutwannheden

@knutwannheden knutwannheden commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Running org.openrewrite.java.logging.slf4j.Log4j1ToSlf4j1 on a multi-module Maven project that uses Log4j 1 left it not compiling: the sources were rewritten to org.slf4j.Logger and LoggerFactory, but no module got a dependency on org.slf4j:slf4j-api. Log4j2ToSlf4j1 has the same gap.

Both recipes now add slf4j-api 1.x to each module whose sources use the logging API being migrated, and leave the other modules alone. For Log4j2ToSlf4j1:

 <dependencies>
     <dependency>
         <groupId>org.apache.logging.log4j</groupId>
         <artifactId>log4j-api</artifactId>
         <version>2.24.3</version>
     </dependency>
+    <dependency>
+        <groupId>org.slf4j</groupId>
+        <artifactId>slf4j-api</artifactId>
+        <version>1.7.36</version>
+    </dependency>
 </dependencies>

Log4j1ToSlf4j1 needs its own entry

Log4j1ToSlf4j1 runs Log4j1ToLog4j2 and then Log4j2ToSlf4j1, so the entry in Log4j2ToSlf4j1 looks like it should cover both. It does not. AddDependency matches onlyIfUsing against the sources as they were before the recipe ran, and those still import org.apache.log4j, so org.apache.logging.log4j.* matches nothing. Log4j1ToSlf4j1 therefore carries a second entry keyed on org.apache.log4j.*.

Tests

Each recipe gets an addSlf4jApiDependency test on a Maven project that declares only the Log4j dependency. Mutation-checked: with both entries removed exactly those two tests fail, and with only the Log4j1ToSlf4j1 entry removed its test fails.

The Log4j 1 test asserts that the pom contains the slf4j-api block instead of comparing the whole file, because Log4j1ToLog4j2 also writes the latest Log4j 2 version into it.

Not changed here

The Log4j 2 dependencies stay in the pom and no SLF4J binding is added. The migrated project compiles, but where its log output goes is still left to the project.

🤖 Generated with Claude Code

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@knutwannheden
Add slf4j-api when migrating Log4j to SLF4J 1.x
72c2c06
`Log4j1ToSlf4j1` and `Log4j2ToSlf4j1` rewrite sources to `org.slf4j.Logger`
and `LoggerFactory` but added no dependency on `org.slf4j:slf4j-api`, so a
project that did not already declare it stopped compiling. Seen on a
multi-module Maven project using Log4j 1.

Both recipes now add `slf4j-api` 1.x to each module whose sources use the
logging API they migrate. `Log4j1ToSlf4j1` needs its own entry because
`onlyIfUsing` sees the sources before the Log4j 1 to Log4j 2 step has run.
@knutwannheden knutwannheden self-assigned this Nov 22, 2026
@moderne-meeseeks moderne-meeseeks Bot added this to OpenRewrite Nov 22, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Nov 22, 2026
timtebeek
timtebeek approved these changes Nov 22, 2026
View reviewed changes
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Nov 22, 2026
@knutwannheden
knutwannheden merged commit 81eb62b into main Nov 22, 2026
1 check passed

Uh oh!

There was an error while loading. Please reload this page.

@knutwannheden
knutwannheden deleted the log4j1toslf4j1-leaves-project-without-slf4j-api branch November 22, 2026 20:14
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@timtebeek timtebeek timtebeek approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@knutwannheden knutwannheden

Labels

None yet

Projects

OpenRewrite
Status: Done

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@knutwannheden @timtebeek
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.