The @Slf4j annotation from Lombok generates a private static final org.slf4j.Logger log field in your class at compile time, named after the class. You then call log.info(...), log.error(...) and so on without writing the LoggerFactory.getLogger(...) boilerplate yourself.
It is a convenience for code that already uses SLF4J as its logging facade. It does not add a logging implementation: you still need Logback, Log4j 2 or another SLF4J binding on the classpath.
@Slf4j generatestopic@Slf4j generatesWithout Lombok, every class that logs declares its own logger:
With Lombok, the same class becomes:
Lombok's annotation processor adds the log field while the compiler runs, so the source stays short and the bytecode is identical to the hand-written version. The generated field is private static final, so one logger is shared by all instances.
You need two things: the Lombok dependency (compile-time only) and a logging backend. Spring Boot's spring-boot-starter-logging already brings SLF4J with Logback.
With Gradle, declare it as compileOnly and also as annotationProcessor:
Since JDK 23, annotation processing is no longer enabled implicitly for processors found only on the classpath. Declare Lombok explicitly as an annotation processor (Gradle's annotationProcessor, or annotationProcessorPaths in the Maven compiler plugin) and use a current Lombok release, otherwise log will be reported as an unknown symbol.
topicBy default the logger name is the fully qualified class name. The topic attribute overrides it, which is useful for grouping related classes under one logger name:
You can then configure the audit logger separately in logback.xml or application.properties, for example logging.level.audit=DEBUG.
Lombok supports several logging frameworks. Pick the one that matches the facade your project uses.
| Annotation | Generated field type |
|---|---|
@Slf4j | org.slf4j.Logger |
@Log4j2 | org.apache.logging.log4j.Logger |
@Log | java.util.logging.Logger |
@CommonsLog | org.apache.commons.logging.Log |
@JBossLog | org.jboss.logging.Logger |
@XSlf4j | org.slf4j.ext.XLogger |
Prefer @Slf4j in new code: SLF4J is the de facto facade, and it lets you swap the backend without touching source.
The generated field is a normal SLF4J logger, so the usual rules apply.
log.debug("user={}", id)) instead of string concatenation. The message is only formatted when the level is enabled.log.error("Payment failed for {}", orderId, ex).log.atDebug().setMessage("state={}").addArgument(() -> expensive()).log().log unless you set lombok.log.fieldName in lombok.config; the same file can make the field non-static with lombok.log.fieldIsStatic = false.@Slf4j cannot be used on interfaces or on classes where a log field already exists.log as unresolved even though the build works.@Slf4j include a logging library?No. It only references the SLF4J API. You must have a binding such as Logback on the runtime classpath, otherwise SLF4J prints a warning and discards messages.
@Slf4j in a Spring Boot application?Yes. Spring Boot ships SLF4J and Logback by default, so adding Lombok is enough.
log not recognised in my IDE?Annotation processing is usually disabled or the Lombok plugin is missing. Enable annotation processing in the project settings and make sure Lombok is on the processor path.
Rarely. If you need to assert on log output, attach a test appender (for example Logback's ListAppender) to the class's logger instead of mocking the field.
@Slf4j removes the repetitive logger declaration by generating a private static final SLF4J log field at compile time. Use topic to rename the logger, keep Lombok configured as an annotation processor on current JDKs, and follow normal SLF4J practice of parameterised messages and passing exceptions as the final argument.