javaspringspring-bootslf4jslf4j-api

Using Simple Logging Facade for Java n a SpringBoot app


I want to use the @Slf4j annotation, so I imported this dependency in my pom.xml file

<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.29</version>
        </dependency>

but I have the error Cannot resolve symbol Slf4j

@Service
@Slf4j
@Transactional(readOnly = true)
public class PasswordResetTokenService {
..
}

Solution

  • The annotation @Slf4j is a Lombok annotation and is not present in the slf4j dependency.

    If you want to use this annotation instead of declaring a logger field, you will need to add an extra dependency to Lombok:

    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.10</version>
        <scope>provided</scope>
    </dependency>
    

    In case of Spring Boot, the parent POM might already specify the version. Then you don't need to declare the specific version anymore.

    See:

    https://projectlombok.org/features/log

    https://projectlombok.org/api/lombok/extern/slf4j/Slf4j.html