0
0
SpringbootHow-ToBeginner · 3 min read

How to Use @Slf4j in Spring Boot for Logging

In Spring Boot, you can use the @Slf4j annotation from Lombok to add a logger to your class automatically. Just add @Slf4j above your class, and then use log.info(), log.error(), etc., inside your methods to log messages.
📐

Syntax

The @Slf4j annotation is placed above a class to create a logger instance named log. This logger can then be used to write log messages at different levels like info, debug, warn, and error.

  • @Slf4j: Lombok annotation to generate a logger.
  • log: The logger instance created automatically.
  • log.info(), log.error(), etc.: Methods to log messages at various levels.
java
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MyClass {
    public void doSomething() {
        log.info("This is an info log message");
    }
}
💻

Example

This example shows a Spring Boot service class using @Slf4j to log messages when a method is called. It demonstrates how easy it is to add logging without manually creating a logger.

java
package com.example.demo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class GreetingService {

    public String greet(String name) {
        log.info("Greet method called with name: {}", name);
        if (name == null || name.isEmpty()) {
            log.warn("Empty or null name received");
            return "Hello, Guest!";
        }
        return "Hello, " + name + "!";
    }
}
Output
INFO 12345 --- [ main] c.e.demo.GreetingService : Greet method called with name: Alice Hello, Alice!
⚠️

Common Pitfalls

Common mistakes when using @Slf4j include:

  • Not adding Lombok dependency or annotation processor, so @Slf4j does not work.
  • Trying to create a logger manually when @Slf4j already provides one.
  • Using the wrong logger name instead of log.

Make sure Lombok is set up correctly in your project and use the log variable generated by @Slf4j.

java
/* Wrong way: manually creating logger when using @Slf4j */
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class WrongExample {
    // This is redundant and unnecessary
    private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(WrongExample.class);

    public void test() {
        logger.info("Using manual logger");
        log.info("Using @Slf4j logger");
    }
}
📊

Quick Reference

  • Add Lombok dependency to your pom.xml or build.gradle.
  • Annotate your class with @Slf4j.
  • Use log.info(), log.debug(), log.error(), etc., to log messages.
  • Ensure your IDE supports Lombok annotation processing.

Key Takeaways

Add @Slf4j above your class to get a logger named log automatically.
Use log.info(), log.debug(), log.error() to write log messages easily.
Make sure Lombok is included and annotation processing is enabled in your project.
Avoid manually creating loggers when using @Slf4j to keep code clean.
Logging helps track application behavior and troubleshoot issues effectively.