Converting your Logger Declarations to Lombok Annotations
Without lombok, loggers are declared in their own field, e.g. private static final Logger log = LoggerFactory.getLogger(FooService.class);
.
This declaration takes the class it is declared in as a parameter, which a developer might forget to change when copying classes.
Other than this parameter the declaration is the same for every class, making it boilerplate code.
Lombok can make your code more concise with a class level annotation, e.g. @Slf4
There are different steps to migrate to the lombok annotation depending on the specifics of your project:
Ensuring all Fields have the same name
The loggers generated by Lombok will all have the same name (by default log
), so the loggers in your project should have the same name as well.
Since the declaration is uniform you can use regular expressions to find outliers.
If you have a linux console grep
and friends might be sufficient, see Example A[1].
Logger
. (There are other types(!), e.g. just Log
).grep -oh "Logger \w* =" -r --include=*.java | awk '{ print $2}' | sort | uniq -c
Ensuring your name matches the generated name
By default, the loggers generated by Lombok will all have the name log
.
If you are happy to use the default name log
, make sure that your logger fields are all called log
in their declaration and usages.
If you want to keep using a different name you can configure that in your lombok.config
.
If your fields are all called LOGGER
you would need to add lombok.log.fieldName=LOGGER
to your lombok.config
.
Converting Field declarations into Annotations
Now that the repository has been prepared, we can apply a recipe:
If you are using SLF4J as a logger you can follow the steps below depending on your build system.
For Log4j2
, or the loggers from java.util
, org.jboss
or apache.commons
there are recipes as well.
You can find them via the sidebar on the left.
Lombok supports more logging frameworks like google’s Flogger
but there are no recipes for them yet.
-
Maven POM
-
Maven Command Line
-
Gradle
-
Gradle init script
-
Add the following to your pom.xml file:
pom.xml<project> <build> <plugins> <plugin> <groupId>org.openrewrite.maven</groupId> <artifactId>rewrite-maven-plugin</artifactId> <version>5.42.2</version> <configuration> <activeRecipes> <recipe>io.github.timoa.lombok.log.ConvertSlf4j</recipe> </activeRecipes> </configuration> <dependencies> <dependency> <groupId>io.github.timo-a</groupId> <artifactId>rewrite-recipe-starter</artifactId> <version>0.4.2</version> </dependency> </dependencies> </plugin> </plugins> </build> </project>
-
Run
mvn rewrite:run
to run the recipe.
You will need to have Maven installed on your machine before you can run the following command.
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=io.github.timo-a:rewrite-recipe-starter:RELEASE -Drewrite.activeRecipes=io.github.timoa.lombok.log.ConvertSlf4j
-
Add the following to your
build.gradle
file:build.gradleplugins { id("org.openrewrite.rewrite") version("6.25.1") } rewrite { activeRecipe("io.github.timoa.lombok.log.ConvertSlf4j") } repositories { mavenCentral() } dependencies { rewrite("io.github.timo-a:rewrite-recipe-starter:0.4.2") }
-
Run
gradle rewriteRun
to run the recipe.
-
Create a file named
init.gradle
in the root of your project.init.gradleinitscript { repositories { maven { url "https://plugins.gradle.org/m2" } } dependencies { classpath("org.openrewrite:plugin:6.25.1") } } rootProject { plugins.apply(org.openrewrite.gradle.RewritePlugin) dependencies { rewrite("io.github.timo-a:rewrite-recipe-starter:0.4.2") } rewrite { activeRecipe("io.github.timoa.lombok.log.ConvertSlf4j") } afterEvaluate { if (repositories.isEmpty()) { repositories { mavenCentral() } } } }
-
Run
gradle --init-script init.gradle rewriteRun
to run the recipe.
awk
prints the word in the middle, sort
prepares the data for uniq
which displays the counts.