@Slf4j is a Lombok annotation that generates a static, final SLF4J Logger field named log in your class at compile time. It is not part of Spring Boot itself, but it is widely used in Spring Boot projects because it removes the boilerplate line that creates a logger in every class. Spring Boot's default logging stack, Logback behind the SLF4J API, picks up the messages with no extra setup.
Without Lombok, every class that logs needs a field like this:
With @Slf4j, Lombok's annotation processor adds exactly that field for you during compilation. You write only the annotation.
The log variable does not appear in your source, but it exists in the compiled class, and the logger name is the fully qualified class name. That name is what you use to set levels per package.
Spring Boot manages the Lombok version, so you do not specify one.
Maven:
Gradle:
You also need the Lombok plugin or built-in annotation processing enabled in your IDE so it understands the generated field. On recent JDKs (23 and later), annotation processors on the classpath are no longer run implicitly, so declare Lombok explicitly as an annotation processor path in Maven or use the Gradle annotationProcessor configuration as shown. Use a current Lombok release, because older versions do not support the newest JDKs.
SLF4J uses {} placeholders, which avoid building strings when a level is disabled.
Avoid string concatenation such as "Loaded " + items.size(), which allocates the string even when debug is off. To log an exception with its stack trace, pass it as the last argument, as in the log.error call above. SLF4J 2.x, used by Spring Boot 3, also offers a fluent API with structured key-value pairs:
Whether key-value pairs appear in the output depends on your Logback encoder or pattern.
Set levels in application.yml, using the logger names that @Slf4j creates:
| Level | Typical use |
|---|---|
ERROR | A failure the operator must look at |
WARN | Something unexpected that the app recovered from |
INFO | Important lifecycle and business events |
DEBUG | Detail useful during diagnosis |
TRACE | Very fine-grained flow |
Do not log secrets, tokens or full personal data, whatever the level.
Lombok offers one annotation per logging framework. Pick the one that matches the framework your project uses.
| Annotation | Logger type created |
|---|---|
@Slf4j | SLF4J |
@Log4j2 | Log4j 2 API |
@Log | java.util.logging |
@CommonsLog | Apache Commons Logging |
@XSlf4j | SLF4J extended logger |
In Spring Boot projects @Slf4j is the natural choice because the starter already provides SLF4J and Logback.
Change the generated name with a lombok.config file at the project root:
To use a custom logger name, for example to group audit messages, pass the topic:
No. It comes from Lombok. Spring Boot supplies SLF4J and Logback through spring-boot-starter-logging, which spring-boot-starter already includes.
The IDE is not running Lombok's annotation processor. Enable annotation processing in the IDE settings and make sure the Lombok plugin is installed or current.
Both produce the same compiled result. Use @Slf4j if your team already uses Lombok, and a manual field if you want to avoid a compile-time dependency.
Yes. The logger is a static field, so proxies and subclassing do not affect it.
@Slf4j generates a private static final SLF4J logger named log, so you can log immediately and control levels through Spring Boot's logging.level properties. Add Lombok as an annotation processor, log with placeholders, pass exceptions as the last argument, and keep sensitive data out of messages.