In this post, I will show you how to reduce boilerplate code while logging in to Java applications using Project Lombok @Slf4j annotation with an example.
What is @Slf4j?
The @Slf4j annotation is a part of Project Lombok which stands for Simple Logging Facade for Java (SLF4J). When you add this annotation to a class, Lombok automatically generates a static logger instance for that class, which you can use without any additional configuration.
The magic here is the log variable. With just the @Slf4j annotation, Lombok creates this logger instance for you.
Project Lombok Maven
- Create a simple maven project using the - How to Create a Simple Maven Project in Eclipse article.
- Add the below dependency in your maven project pom.xml file:
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>latest-version</version>
<scope>provided</scope>
</dependency>
Adding the Lombok Plugin in IDE (Eclipse)
Follow the below steps to install Lombok in Eclipse for Windows:
- Downloaded the jar from https://projectlombok.org/download or use the jar which is downloaded from your maven build.
- Execute command in terminal: java -jar lombok.jar
- This command will open the window as shown in the picture below, install and quit the installer and restart Eclipse.
Without Project Lombok
First, let's see how the code looks without @Slf4j Lombok annotation:
package net.javaguides.lombok.logging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.javaguides.lombok.User;
public class UserService {
public static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);
public void saveUser() {
LOGGER.debug("inside saveUser() method");
}
public void updateUser() {
LOGGER.debug("inside updateUser() method");
}
public User findUser(long id) {
LOGGER.debug("inside findUser() method");
LOGGER.info(new Long(id).toString());
return null;
}
public void deleteUser(long id) {
LOGGER.debug("inside deleteUser() method");
LOGGER.info(new Long(id).toString());
}
}
With Project Lombok
Let's re-write the above code using Project Lombok @Slf4j annotation:package net.javaguides.lombok.logging;
import lombok.extern.slf4j.Slf4j;
import net.javaguides.lombok.User;
@Slf4j
public class UserServiceLombok {
public void saveUser() {
log.debug("inside saveUser() method");
}
public void updateUser() {
log.debug("inside updateUser() method");
}
public User findUser(long id) {
log.debug("inside findUser() method");
log.info(new Long(id).toString());
return null;
}
public void deleteUser(long id) {
log.debug("inside deleteUser() method");
log.info(new Long(id).toString());
}
public static void main(String[] args) {
UserServiceLombok userService = new UserServiceLombok();
userService.saveUser();
userService.updateUser();
userService.deleteUser(100 L);
userService.findUser(100 L);
}
}
Below is some others annotation for other Logging frameworks:
- @CommonsLog: org.apache.commons.logging.Log
- @JBossLog: org.jboss.logging.Logger
- @Log: java.util.logging.Logger
- @Log4j: org.apache.log4j.Logger
- @Log4j2: org.apache.logging.log4j.Logger
- @XSlf4j: org.slf4j.ext.XLogger
Check out more examples at https://projectlombok.org/features/log
How can I mock @Slf4j during the unit tests ?
ReplyDeletethis is only difference but i want how to create log step by step using lombok.
ReplyDelete