@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 field's type depends on which logger you have selected.

There are four 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);
@Slf4j
Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);

With Lombok

01 import lombok.extern.slf4j.Log;
02 
03 @Log
04 public class LogExample {
05   
06   public static void main(String... args) {
07     log.error("Something's wrong here");
08   }
09 }
10 
11 @Log(java.util.List.class)
12 public class LogExampleOther {
13   
14   public static void main(String... args) {
15     log.warn("Something might be wrong here");
16   }
17 }

Vanilla Java

01 public class LogExample {
02   private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
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(java.util.List.class);
11   
12   public static void main(String... args) {
13     log.warn("Something might be wrong here");
14   }
15 }

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.