Search code examples
javalombok

Adding lombok dependency and @Slf4j does not let use logger


I want to use logger from Lombok. I added @Slf4j annotation, added dependency and it says that it cannot resolve symbol log. Error:(5, 1) java: package org.slf4j does not exist

package a;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class a {
    public static void main(String[] args) {
        log.info("lala");
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>a</groupId>
    <artifactId>a</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

Solution

  • You also need to add slf4j itself as a dependency to your project by including it in your pom file. All lombok features in the lombok.extern package share this property: They help you use a library that is NOT already available out of the box as part of java itself, and for all of them, lombok does not inherently include any of these dependencies.

    Should be as simple as adding the following block to your pom.xml:

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