In this tutorial I will show you how to configure Lombok for logging using @slf4j annotation in Java, Spring and Spring Boot applications. The example shown here will work for Java, Spring and Spring Boot applications. This annotation will reduce the code you write for creating logger variable. Lombok will provide log variable when you use @slf4j annotation on class level. You can use this log variable to create log statements.

I am going to use log4j2 with slf4j API for Java, Spring and Spring Boot applications. You can even use spring-boot-starter-log4j2 API instead of log4j2 with slf4j for Spring Boot application. This starter dependency will resolve the similar kind of libraries as log4j2 with slf4j.

At least Java 8, lombok API, log4j2 with slf4j API, Spring, Spring Boot

Create Build Configuration

If you are creating gradle based project in Eclipse then you can use the following dependencies in build.gradle script:

If you are using maven based project then you can use below dependencies in pom.xml file:

The above build configuration for gradle or maven based project will work for Java, Spring and Spring boot applications.

But you need to exclude logback API from Spring Boot starter otherwise it will conflict and logback will override log4j2.

For gradle build:

For maven build:

For Spring Boot applications you can also use only log4j starter API that will resolve the similar libraries. Make sure to exclude the logback API even when you are using log4j starter API. Don’t forget to include the lombok API.

For gradle build:

For maven build:

Create log4j2.xml

Log4j is a logging library for Java based applications and purpose of inserting log statements into code is a low-tech method for debugging it. But make sure you follow the best practices while putting log statements.

Now we will create log4j2.xml file under the classpath directory src/main/resources.

As we are using log4j2 API, so the configuration file name is log4j2.xml or If you are using log4j API version 1, then your file name should be log4j.xml or

Keeping the log4j2 XML or properties file in classpath will be picked up by the application automatically.

The status in the log4j2 configuration is used internally by log4j2 components. This is useful for troubleshooting the configuration issues. Here I have turned this off. You can set debug, warn etc. for the status.

Then we set some properties with key/value pairs.

Next we define two appenders – RollingFile and Console.

In RollingFile appender we set the file name and file name pattern. We have maximum backup file 5 apart from main.log file. The file name pattern will create main.1.log … main.5.log and another main.log file will be the current log file.

We have specified the pattern of the log statements.

Next we have specified policy to start a new log file whenever the application starts up. During start up the file name pattern comes into picture and maximum 5 files will be created apart from main log file.

The console appender prints the log statements into console.

Next we define some loggers with level of logging.

The root level of logging is info for file as well as console logging.

Avoid duplicate logging by adding aditivity=”false”.

Example of Logging

The below class shows how to create log statements which will be printed in the console as well as into the file.

Notice in the above class, we have just put @slf4j annotation on the class level and log variable is available for creating log statements.

This content was originally published here.