In this post, I will show you how to reduce boilerplate code while logging in Java applications using Project Lombok @Slf4j annotation with an example.
Project Lombok Maven
- Create a simple maven project using - 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>1.18.4</version>
<scope>provided</scope>
</dependency>
Adding the Lombok Plugin in IDE (Eclipse)
Follow below steps to install Lombok in eclipse for Windows:
- Downloaded 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 window as shown in the picture below, install and quit the installer and restart eclipse.
Without Project Lombok
Let's write a source code without using Project Lombok:
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 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
Reference
GitHub Repository
You can view the source code of this article on my GitHub repository at https://github.com/RameshMF/project-lombok-tutorial
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course