33

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>
1
  • 2
    Your are simply missing the slf4j API. You need to add it as a dependency. :) Commented Jul 19, 2019 at 13:16

5 Answers 5

45

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>
Sign up to request clarification or add additional context in comments.

7 Comments

This worked for me. Although in a spring boot, I just added lombok and it worked. In a standalone project I needed to add sl4j along with lombok. Maybe Spring Boot automatically gets all lombok's extern packages or something.
@theprogrammer I believe spring boot included slf4j, specifically.
When we include spring-boot-starter-web or spring-boot-starter-data-jpa dependency then slf4j comes automatically with them.
Actually springboot provides implementation of Slf4j for you. When you add lombok to apps or modules without springboot - you don't have the implementation provided.
I was working in my SpringBoot project few weeks (no slf4j in pom) and one day I started getting the same exception during compilation in IDE. Maven packaging was working correctly. I had to remove all IntelliJ generated files from project and import project (from pom) from scratch.
|
2

I tried adding slf4j-api as a dependency as rzwitserloot suggested above, but that did not work for me. Adding it as a plugin in my build.gradle file did, though:

plugins {
    ...
    id 'io.freefair.lombok' version '4.1.6'
    ...
}

Comments

1

upgrade the version of the spring-boot-starter-parent. it worked for me. Got o start.spring.io and you will see the latest springboot version, as in: 4.0.0

Comments

1

in recent time, you can just initialize logger before you use it: Logger log = LoggerFactory.getLogger(<Class_Name>.class);

3 Comments

... the whole point of @Slf4j (or just any lombok annotation in general) is to reduce boilerplate code. This is like suggesting creating every getter and setter if @Data isn't working. Recommended deletion
format your answer for clarity and state which file should the op apply the change in
it is more like using constructor dependency Injection instead of @Autowired annotation. in my case, I upgraded my springboot version, added Slf4j dependency and still nothing worked. what you can easily do is add "Logger log = LoggerFactory.getLogger(<Class_Name>.class);" to any class you want to use log before adding whatever method.
-1

You'll need to install the Lombok plugin in IntelliJ(IDE) if you want IntelliJ to recognize Lombok annotations. and restart your IDE to reflact the changes.

3 Comments

The asker made absolutely no mention of intellij, and the error makes entirely clear that lombok is working fine, but the slf4j dependency isn't there, thus proving this answer is irrelevant.
This problem is IDE-agnostic.
Issue is not with intellij. It is a compilation error issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.