1. Overview

In this tutorial, we're going to examine Lombok log annotations like @Slf4j, @Log4j or @Log.

2. Use Log Annotations

Lombok provides several log annotations to work with different logging libraries. In the end, they all generate a logger instance named as log which we can use in our methods.

2.1. Use @CommonsLog

@CommonsLog generates a logger for the Apache Commons Logging library.

Firstly let's add the commons-logging Maven dependency:

<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.1.1</version>
</dependency>

Now we can annotate our classes with @CommonsLog:

@CommonsLog
public class CommonsLogClient {

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

Here, we're accessing the logger with the log variable.

Lombok generates this log variable during the compilation.

public class CommonsLogClient {

    private static final Log log = org.apache.commons.logging.LogFactory.getLog(CommonsLogClient.class);

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

2.2. Use @Slf4j

@Slf4j generates a logger using the SLF4J API. In order to create a logger instance, we must also include a library that implements the Slf4j API.

We'll use the Logback library:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

The logback-classic dependency pulls in other required dependencies.

Now, we can annotate our class with @Slf4j:

@Slf4j
public class Slf4jClient {

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

This, in return, generates a Logback-backed logger:

public class Slf4jClient {

    private static final Logger log = org.slf4j.LoggerFactory.getLogger(Slf4jClient.class);

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

2.3. Use @Log4j

@Log4j creates a logger using the log4j library.

We'll add the log4j Maven dependency:

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

Similar to previous examples we must annotate our class:

@Log4j
public class Log4jClient {

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

Then Lombok generates the logger accordingly:

public class Log4jClient {

    private static final Logger log = Logger.getLogger(Log4jClient.class);

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

2.4. Use @Log4J2

We can also use @Log4j2 to use the newer version of log4j.

Firstly, we must pull in the required dependencies:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.12.1</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.12.1</version>
</dependency>

When we annotate our class with @Log4j2:

@Log4j2
public class Log4j2Client {

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

Lombok generates:

public class Log4j2Client {

    private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(Log4j2Client.class);

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

2.5. Use @FLogger

The FLogger is the standard logging library used in Google. @FLogger creates an instance of FLogger.

We can add FLogger to our application through Maven:

<dependency>
    <groupId>com.google.flogger</groupId>
    <artifactId>flogger</artifactId>
    <version>0.4</version>
</dependency>
<dependency>
    <groupId>com.google.flogger</groupId>
    <artifactId>flogger-system-backend</artifactId>
    <version>0.4</version>
</dependency>

flogger and flogger-system-backend are the required dependencies.

When a class is annotated with @FLogger:

@Flogger
public class FLoggerClient {

    public static void main(String[] args) {
        log.atSevere().withCause(new RuntimeException("Planned")).log("Error occurred");
    }
}

Lombok generates:

public class FLoggerClient {

    private static final FluentLogger log = FluentLogger.forEnclosingClass();

    public static void main(String[] args) {
        log.atSevere().withCause(new RuntimeException("Planned")).log("Error occurred");
    }
}

2.6. Use @JBossLog

We can also use @JBossLog for logging purposes.

To use it, we must first add the jboss-logging Maven dependency:

<dependency>
    <groupId>org.jboss.logging</groupId>
    <artifactId>jboss-logging</artifactId>
    <version>3.4.0.Final</version>
</dependency>

Then we must annotate our class with @JBossLog:

@JBossLog
public class JBossLogClient {

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

This results in:

public class JBossLogClient {

    private static final Logger log = Logger.getLogger(JBossLogClient.class);

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

2.7. Use @Log

@Log creates a logger using Java util logging. To use it, we don't need an additional dependency.

We must annotate our class with @Log:

@Log
public class LogClient {

    public static void main(String[] args) {
        log.log(Level.SEVERE, "Error occurred", new RuntimeException("Planned"));
    }
}

Lombok generates a logger:

public class LogClient {

    private static final Logger log = Logger.getLogger(LogClient.class.getName());

    public static void main(String[] args) {
        log.log(Level.SEVERE, "Error occurred", new RuntimeException("Planned"));
    }
}

2.8. Use @XSlf4j

@XSlf4j creates a logger using Slf4j extensions.

We must add the slf4j-ext Maven dependency:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-ext</artifactId>
    <version>1.7.27</version>
</dependency>

Then we annotate the class with @XSlf4j:

@XSlf4j
public class XSlf4jClient {

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

As a result, the logger is generated:

public class XSlf4jClient {

    private static final XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(XSlf4jClient.class);

    public static void main(String[] args) {
        log.error("Error occurred", new RuntimeException("Planned"));
    }
}

3. Set the Logger Topic

We can change the logger category with the topic attribute. By default, it is the enclosing class name.

@CommonsLog(topic = "integration")
@JBossLog(topic = "integration")
@Log4j(topic = "integration")
@Log4j2(topic = "integration")
@Log(topic = "integration")
@Slf4j(topic = "integration")
@XSlf4j(topic = "integration")
public class SpecifyingTopic {
    public static void main(String[] args) {
        log.error("Error occurred");
    }
}

4. Global Configuration

Lombok provides several configuration properties for log annotations.

4.1. lombok.log.fieldName

By default, the logger field name is log. But we can change the field name using the lombok.log.fieldName property:

# an identifier (default: log).
lombok.log.fieldName = logger

We're changing the name as logger.

4.2. lombok.log.fieldIsStatic

Lombok generates the logger as a static field in the class. If we set lombok.log.fieldIsStatic to false, it will be an instance field instead:

# [true | false] (default: true)
lombok.log.fieldIsStatic = false

4.3. lombok.log.flagUsage

lombok.log.flagUsage flags the usages of log annotations.

For example, we can forbid the use of any log annotations by setting its value to error:

# [warning | error] (default: not set)
lombok.log.flagUsage = error

Note that this property doesn't have a default value.

4.4. Library Specific Flag Usage

We can also flag the usages of the library-specific annotations.

# [warning | error] (default: not set)
lombok.log.apacheCommons.flagUsage = error

# [warning | error] (default: not set)
lombok.log.flogger.flagUsage = error

# [warning | error] (default: not set)
lombok.log.jbosslog.flagUsage = error

# [warning | error] (default: not set)
lombok.log.javaUtilLogging.flagUsage = error

# [warning | error] (default: not set)
lombok.log.log4j.flagUsage = error

# [warning | error] (default: not set)
lombok.log.log4j2.flagUsage = error

# [warning | error] (default: not set)
lombok.log.slf4j.flagUsage = error

# [warning | error] (default: not set)
lombok.log.xslf4j.flagUsage = error

5. Summary

In this tutorial, we looked at various log annotations Lombok provides.

Finally, check out the source code for all examples over on Github.