What is @Slf4j?
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)
- 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
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);
}
}
- @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
Conclusion
Lombok's @Slf4j annotation makes logging in Java applications cleaner and more concise. It abstracts the repetitive logger instantiation, allowing developers to focus on the essential aspects of logging. Remember, while Lombok simplifies logging, you should still follow best practices for logging levels, formats, and messages to make the most out of your logs.Reference
GitHub Repository
You can view the source code of this article on my GitHub repository at https://github.com/RameshMF/project-lombok-tutorialContinue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
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