In a Spring Boot application, @Slf4j is a Lombok annotation that generates a ready-to-use SLF4J logger field named log in your class. It replaces the line private static final Logger log = LoggerFactory.getLogger(MyClass.class);, so you can write log.info(...) immediately. It does not do any logging itself: output still goes through the logging backend Spring Boot configures, Logback by default.
Annotating a class with @Slf4j makes Lombok add this field at compile time:
The logger name is the fully qualified class name, which is what Spring Boot's logging.level.* properties match against. Nothing is added at runtime, and the generated code is identical to what you would write by hand.
Spring Boot manages the Lombok version, so you do not specify one. With Maven:
With Gradle:
The SLF4J API itself arrives through spring-boot-starter-logging, which every starter includes, together with Logback. If you build without Boot, add slf4j-api and a backend yourself.
On JDK 23 and later, javac no longer runs annotation processors found only on the classpath by default. If log cannot be resolved after upgrading, declare Lombok under annotationProcessorPaths in the Maven compiler plugin (Gradle's annotationProcessor configuration already does this) or enable processing explicitly.
A request to /orders/7 prints something like this with the default Spring Boot pattern:
Note two habits in the example. Placeholders ({}) avoid string concatenation and only format when the level is enabled. Passing the exception as the last argument logs the full stack trace, which ex.getMessage() does not.
Set levels per package or class in application.properties:
Use log.isDebugEnabled() only around work that is expensive to prepare. For simple values, placeholders are enough.
| Option | Effect |
|---|---|
@Slf4j(topic = "audit") | Uses the name audit instead of the class name |
lombok.log.fieldName = logger in lombok.config | Renames the field project-wide |
lombok.log.fieldIsStatic = false in lombok.config | Generates an instance field instead of a static one |
lombok.log.custom.declaration | Defines a custom logger declaration |
Use a topic when you want a dedicated logger for audit or security events that you can route to its own appender.
Lombok has one annotation per framework: @Log (java.util.logging), @Log4j2, @CommonsLog, @JBossLog, @Flogger and @XSlf4j. In Spring Boot, @Slf4j is the usual choice because it matches the logging facade Boot uses, and it works with both Logback and Log4j2 backends.
@Slf4j is a class-level annotation. Put it on each class that logs, not on a base class unless subclasses should share the logger name.log.log fields in a class hierarchy are fine, but a field already named log makes Lombok skip generation and warn.No. It needs only the SLF4J API. Any compatible backend works, and Spring Boot defaults to Logback.
It is Lombok's default for all logging annotations. Change it with lombok.log.fieldName in lombok.config.
No. The compiled bytecode is the same as the hand-written field.
Lombok support for @Slf4j on records depends on the Lombok version. If it is unavailable, declare the logger manually in the record.
@Slf4j generates a static SLF4J logger named log at compile time, saving boilerplate in Spring Boot classes. Add Lombok as compile-only, log with placeholders, pass exceptions as the last argument, and configure levels through Spring Boot properties.