Java Lombok | Using Logger - @Slf4j

Using Logger

If you declare the @Slf4j annotation on a class, you can use a Logger through the log variable declared as static final.

First, add the Logger library to build.gradle.

dependencies {

    .. 생략 ...
        
 +   compile 'org.slf4j:slf4j-simple:1.7.30'
 }

The code below is an example that uses @Slf4j.

package com.devkuma.tutorial.lombok;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LoggerTutorial {

    public static void main(String[] args) {
        log.info("Hello Lombok Logger!!");
    }
}

Execution result:

[main] INFO com.devkuma.tutorial.lombok.LoggerTutorial - Hello Lombok Logger!!

Other Provided Annotations

In addition to Slf4j, Lombok provides the following Logger annotations.

Annotation Logger class
@CommonsLog org.apache.commons.logging.Log
@Log org.apache.commons.logging.Log
@Log4j org.apache.log4j.Logger
@Log4j2 org.apache.logging.log4j.Logger
@Slf4j org.slf4j.Logger
@XSlf4j org.slf4j.ext.XLogger