Skip to main content

Lombok Logger

1. Logger Annotations in Lombok​

Lombok provides several annotations for automatically generating logger instances:

AnnotationLogger TypeActual Class UsedDefault Package
@Slf4jSLF4Jorg.slf4j.Logger + org.slf4j.LoggerFactoryorg.slf4j
@Log4jLog4j 1.xorg.apache.log4j.Loggerlog4j
@Log4j2Log4j 2.xorg.apache.logging.log4j.Logger + LogManagerlog4j2
@CommonsLogApache Commons Logging (JCL)org.apache.commons.logging.Log + LogFactorycommons-logging
@JBossLogJBoss Loggingorg.jboss.logging.Loggerjboss-logging
@XSlf4jExtended SLF4J (with markers)org.slf4j.ext.XLogger + XLoggerFactoryslf4j-ext
@FloggerGoogle Floggercom.google.common.flogger.FluentLoggergoogle flogger

2. Using Lombok Logger with Spring Boot​

Spring Boot uses SLF4J as the logging façade by default, routing logs through Logback.

So the recommended option is:

✅ @Slf4j — best choice for Spring Boot​

  • Doesn’t depend on a specific logging backend (you can switch to Logback/Log4j2 easily).
  • Fully compatible with Spring Boot’s default logging setup.
  • Clean, standard, and widely used.

Example:

@Slf4j
@RestController
public class DemoController {

@GetMapping("/")
public String home() {
log.info("Hello from controller!");
return "Hi!";
}
}

🟦 @Log4j2 — for when you really need Log4j2 features​

Use this when you want to leverage Log4j2’s advanced capabilities like:

  • asynchronous logging
  • custom filters & layouts
  • high-performance logging under load

If you use @Log4j2 in Spring Boot:

✔ Remove spring-boot-starter-logging ✔ Add spring-boot-starter-log4j2


🟨 @CommonsLog​

  • Mainly for legacy systems still using Apache Commons Logging.
  • Not recommended for new Spring Boot applications unless required by the codebase.

🟥 @JBossLog​

  • For applications running in the JBoss / WildFly ecosystem.
  • Rarely used in Spring Boot unless deploying into JBoss EAP.

🟩 @Flogger​

  • Powerful for structured/typed logging.
  • Less common in Spring Boot.
  • Requires additional bridging if you want unified logging output.

3. Summary: Which Logger Should You Choose?​

ScenarioRecommended Logger
Standard Spring Boot application@Slf4j ✔
Need Log4j2 advanced features@Log4j2
Legacy code / framework constraints@CommonsLog, @JBossLog
Want Google’s structured logging style@Flogger