@Slf4j Lombok Annotation Example


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.

Project Lombok Maven

  1. Create a simple maven project using the - How to Create a Simple Maven Project in Eclipse article.
  2. 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);
    }
}
The magic here is the log variable. With just the @Slf4j annotation, Lombok creates this logger instance for you.

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-tutorial

Comments

  1. How can I mock @Slf4j during the unit tests ?

    ReplyDelete
  2. this is only difference but i want how to create log step by step using lombok.

    ReplyDelete

Post a Comment

Leave Comment