@Log (and friends)

Overview

NEW in lombok 0.10: You can annotate any class with a log annotation to let lombok generate a logger field.
The logger is named log and the field's type depends on which logger you have selected.

There are six choices available:

@CommonsLog
Creates private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LogExample.class);
@Log
Creates private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
@Log4j
Creates private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LogExample.class);
@Log4j2
Creates private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager.getLogger(LogExample.class);
@Slf4j
Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
@XSlf4j
Creates private static final org.slf4j.ext.XLogger log = org.slf4j.ext.XLoggerFactory.getXLogger(LogExample.class);

By default, the topic (or name) of the logger will be the class name of the class annotated with the @Log annotation. This can be customised by specifying the topic parameter. For example: @XSlf4j(topic="reporting").

With Lombok

01 import lombok.extern.java.Log;
02 import lombok.extern.slf4j.Slf4j;
03 
04 @Log
05 public class LogExample {
06   
07   public static void main(String... args) {
08     log.error("Something's wrong here");
09   }
10 }
11 
12 @Slf4j
13 public class LogExampleOther {
14   
15   public static void main(String... args) {
16     log.error("Something else is wrong here");
17   }
18 }
19 
20 @CommonsLog(topic="CounterLog")
21 public class LogExampleCategory {
22 
23   public static void main(String... args) {
24     log.error("Calling the 'CounterLog' with a message");
25   }
26 }

Vanilla Java

01 public class LogExample {
02   private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(LogExample.class.getName());
03   
04   public static void main(String... args) {
05     log.error("Something's wrong here");
06   }
07 }
08 
09 public class LogExampleOther {
10   private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExampleOther.class);
11   
12   public static void main(String... args) {
13     log.error("Something else is wrong here");
14   }
15 }
16 
17 public class LogExampleCategory {
18   private static final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog("CounterLog");
19 
20   public static void main(String... args) {
21     log.error("Calling the 'CounterLog' with a message");
22   }
23 }

Supported configuration keys:

lombok.log.fieldName = an identifier (default: log)
The generated logger fieldname is by default 'log', but you can change it to a different name with this setting.
lombok.log.fieldIsStatic = [true | false] (default: true)
Normally the generated logger is a static field. By setting this key to false, the generated field will be an instance field instead.
lombok.log.flagUsage = [warning | error] (default: not set)
Lombok will flag any usage of any of the various log annotations as a warning or error if configured.
lombok.log.apacheCommons.flagUsage = [warning | error] (default: not set)
Lombok will flag any usage of @lombok.extern.apachecommons.CommonsLog as a warning or error if configured.
lombok.log.javaUtilLogging.flagUsage = [warning | error] (default: not set)
Lombok will flag any usage of @lombok.extern.java.Log as a warning or error if configured.
lombok.log.log4j.flagUsage = [warning | error] (default: not set)
Lombok will flag any usage of @lombok.extern.log4j.Log4j as a warning or error if configured.
lombok.log.log4j2.flagUsage = [warning | error] (default: not set)
Lombok will flag any usage of @lombok.extern.log4j.Log4j2 as a warning or error if configured.
lombok.log.slf4j.flagUsage = [warning | error] (default: not set)
Lombok will flag any usage of @lombok.extern.slf4j.Slf4j as a warning or error if configured.
lombok.log.xslf4j.flagUsage = [warning | error] (default: not set)
Lombok will flag any usage of @lombok.extern.slf4j.XSlf4j as a warning or error if configured.

Small print

If a field called log already exists, a warning will be emitted and no code will be generated.

A future feature of lombok's diverse log annotations is to find calls to the logger field and, if the chosen logging framework supports it and the log level can be compile-time determined from the log call, guard it with an if statement. This way if the log statement ends up being ignored, the potentially expensive calculation of the log string is avoided entirely. This does mean that you should NOT put any side-effects in the expression that you log.