Use SLF4J Parameterized Logging
In this guide we'll look at using OpenRewrite to automatically refactor logging statements to take advantage of performance improvements offered by using slf4j parameterized logging over String concatenation.

Example Configuration

The ParameterizedLogging 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.ParameterizedLogging")
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.ParameterizedLogging</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.
For the full list of changes, see the recipe's reference documentation.
Example Class (Before)
Example Class (After)
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 name, double percent) {
8
logger.debug("Process [" + name + "] is at [" + percent * 100 + "%]");
9
}
10
11
void asInteger(String numberString) {
12
try {
13
Integer i = Integer.valueOf(numberString);
14
} catch (NumberFormatException ex) {
15
logger.warn("some big error: " + ex.getMessage(), ex);
16
}
17
}
18
}
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 name, double percent) {
8
logger.debug("Process [{}] is at [{}%]", name, percent * 100);
9
}
10
11
void asInteger(String numberString) {
12
try {
13
Integer i = Integer.valueOf(numberString);
14
} catch (NumberFormatException ex) {
15
logger.warn("some big error: {}", ex.getMessage(), ex);
16
}
17
}
18
}
Copied!
Last modified 10d ago
Export as PDF
Copy link