Migrate to SLF4J from Log4j
In this guide, we'll use OpenRewrite to perform an automated migration from Apache Log4j 2 (Log4j) to the Simple Logging Facade for Java (SLF4J).

Example Configuration

The Log4jToSlf4j recipe has no required configuration options and can be activated directly after taking a dependency on rewrite-logging-frameworks in your build file:
Gradle
Maven
build.gradle
1
plugins {
2
id("org.openrewrite.rewrite") version("5.12.0")
3
}
4
5
rewrite {
6
activeRecipe("org.openrewrite.java.logging.slf4j.Log4jToSlf4j")
7
}
8
9
repositories {
10
mavenCentral()
11
}
12
13
dependencies {
14
rewrite("org.openrewrite.recipe:rewrite-logging-frameworks:0.9.0")
15
}
Copied!
pom.xml
1
<project>
2
<build>
3
<plugins>
4
<plugin>
5
<groupId>org.openrewrite.maven</groupId>
6
<artifactId>rewrite-maven-plugin</artifactId>
7
<version>4.13.0</version>
8
<configuration>
9
<activeRecipes>
10
<recipe>org.openrewrite.java.logging.slf4j.Log4jToSlf4j</recipe>
11
</activeRecipes>
12
</configuration>
13
<dependencies>
14
<dependency>
15
<groupId>org.openrewrite.recipe</groupId>
16
<artifactId>rewrite-logging-frameworks</artifactId>
17
<version>0.9.0</version>
18
</dependency>
19
</dependencies>
20
</plugin>
21
</plugins>
22
</build>
23
</project>
Copied!
At this point, you're ready to execute the migration by running mvn rewrite:run or gradlew rewriteRun. After running the migration you can inspect the results with git diff (or equivalent), manually fix anything that wasn't able to be migrated automatically, and commit the results.

Before and After

For the full list of changes, see the recipe's reference documentation.
Similar to the SLF4J Migrator, the goal of the Log4jToSlf4j recipe is to significantly reduce the amount of work involved in migrating Log4j to SLF4J. Because OpenRewrite works by leveraging type-attributed Abstract Syntax Trees (AST) representing your source code, the Log4jToSlf4j recipe can overcome several limitations discussed in the SLF4J Migrator guide.

.toString()

Because Log4j logging statements are automatically migrated to use the SLF4J Parameterized Logging equivalent(see "Use SLF4J Parameterized Logging"), messages of type String are supported. Therefore, there is no need to add a .toString() method invocation on the object.
Object Parameters (Before)
Object Parameters (After)
1
import org.apache.log4j.Logger;
2
3
class Example {
4
Logger logger = Logger.getLogger(Example.class);
5
6
void method(Example example) {
7
logger.info(example);
8
logger.info(new Object());
9
logger.info(new StringBuilder("append0"));
10
}
11
}
Copied!
1
import org.slf4j.Logger;
2
import org.slf4j.LoggerFactory;
3
4
class Example {
5
Logger logger = LoggerFactory.getLogger(Example.class);
6
7
void method(Example example) {
8
logger.info("{}", example);
9
logger.info("{}", new Object());
10
logger.info("{}", new StringBuilder("append0"));
11
}
12
}
Copied!

FATAL to ERROR

The Log4j log level FATAL is not supported in SLF4J. Therefore, FATAL level log statements are migrated to ERROR level.
Migrate fatal to error (Before)
Migrate fatal to error (After)
1
import org.apache.log4j.Logger;
2
3
class Example {
4
Logger logger = Logger.getLogger(Example.class);
5
6
void method() {
7
logger.fatal("uh oh");
8
}
9
}
Copied!
1
import org.slf4j.Logger;
2
import org.slf4j.LoggerFactory;
3
4
class Example {
5
Logger logger = LoggerFactory.getLogger(Example.class);
6
7
void method() {
8
logger.error("uh oh");
9
}
10
}
Copied!

Migrate using type information

A method declaring multiple loggers on the same line will have a complete conversion.
Same Line Loggers (Before)
Same Line Loggers (After)
1
import org.apache.log4j.Logger;
2
3
class Example {
4
public void someMethod(Logger l1, Logger l2) {
5
}
6
}
Copied!
1
// Notice the import change Logger to the SLF4J type equivalent:
2
import org.slf4j.Logger;
3
4
class Example {
5
public void someMethod(Logger l1, Logger l2) {
6
}
7
}
Copied!

Handle Exception Migrations

Additionally, Log4j ERROR log level exceptions are migrated to their SLF4J equivalent:
Migrates Exceptions (Before)
Migrates Exceptions (After)
1
import org.apache.log4j.Logger;
2
3
class Example {
4
Logger logger = Logger.getLogger(Example.class);
5
6
void method(String numberString) {
7
try {
8
Integer i = Integer.valueOf(numberString);
9
} catch (Exception e) {
10
logger.error(e.getMessage(), e);
11
}
12
}
13
}
Copied!
1
import org.slf4j.Logger;
2
import org.slf4j.LoggerFactory;
3
4
class Example {
5
Logger logger = LoggerFactory.getLogger(Example.class);
6
7
void method(String numberString) {
8
try {
9
Integer i = Integer.valueOf(numberString);
10
} catch (Exception e) {
11
logger.error("{}", e.getMessage(), e);
12
}
13
}
14
}
Copied!
For the full list of changes, see the recipe's reference documentation.

Known Limitations

The following is a list of known limitations/issues:
    Log4j configuration customizations which do not have an SLF4J equivalent cannot be migrated.
We are always looking for feedback on which tasks should be prioritized. If you have a specific use case that is not yet covered by this project, please reach out to our team!
Last modified 10d ago