🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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
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-tutorialMy Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
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