[ERROR]: cannot find symbol variable log when building with maven and lombok
Asked Answered
K

4

11

I am trying to build a Java 11 project with maven and lombok's @Slf4j Logger, but maven does not recognize the log variables. IntelliJ does though and is able to build the project.

The error is

[ERROR]: cannot find symbol variable log 

Project and Module SDK is both Java 11. Lombok Version is 1.18.2:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
    <scope>provided</scope>
</dependency>

My maven compiler setup:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>11</source>
                <target>11</target>
                <forceJavacCompilerUse>true</forceJavacCompilerUse>
                <annotationProcessorPaths>
                    <path>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                        <version>1.18.12</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

I already tried:

  • turning Annotaion Processing off and on again
  • reinstalling Lombok plugin
  • clearing .m2/repository folder
  • manually adding lombok.jar as Annotation Processor
  • adding Lombok path to maven-compiler-plugin list of Annotation Processor
Kathlenekathlin answered 28/4, 2020 at 10:45 Comment(12)
Does this answer your question? Building with Lombok's @Slf4j and Intellij: Cannot find symbol logChampion
@Champion no, as I said, I already tried those answers.Kathlenekathlin
what is your maven compiler setup?Nodical
@DarrenForsythe added to questionKathlenekathlin
Assume there;s no other annotation processors kicking around? e.g. lombok + mapstract was always a good gotchaNodical
@karottenbunker IntelliJ version, you are using? Just comment out the <annotationProcessorPaths> configuration in your code and try. By some experience I wrote this comment to a similar configuration in my code <!--contradictory to maven, intelliJ fails with this-->Pleistocene
@DarrenForsythe no, just lombok.Kathlenekathlin
@Pleistocene it still does not work.Kathlenekathlin
Of your bullet points 1) and 2) only relate to the IDE 4) and 5) are not requiredOtalgia
[ERROR]: cannot find symbol variable log @OtalgiaKathlenekathlin
I missed the slf4j depedency but it didn't complain about a missing import though, maybe because of lombok?Kathlenekathlin
@karottenbunker Interesting. Added my (educated) guess as an answerOtalgia
W
7

This is a really minimal example configuration for using the @Slf4j lombok logging annotation.

You need a logging facade and an implementation, in this case I'm going to use slf4j (as facade) and logback (as implementation).


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>org.example</groupId>
    <artifactId>untitled</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>

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

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>
</project>   

main.java

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Main {

    public static void main(String[] args) {
        log.debug("Hello");
    }
}

If you get some trouble try always to force the maven dependencies updates running in your project folder mvn -U clean package and reimporting maven project in your IDE

See screenshot

Wriggler answered 28/4, 2020 at 12:23 Comment(1)
Strange how that "reimport maven modules" button doesn't exist in my IntellijJ...? Also, nothing of what you're saying here makes any kind of difference.Elvia
O
3

My suspicion is that this is a misleading error message as a consequence of the point that Lombok hooks in during compilation.

In bytecode, there is no concept of an import. Classes are replaced by their fully qualified names (e.g. Integer to java.lang.Integer). Therefore at some point in compilation, the imports are parsed, applied, and any unknown classes (e.g. due to lack of the correct dependency) will throw an error at this stage.

Since @Slf4j means you do not need to import org.slf4j.Logger, the step described above is missed for this class.

After Lombok has appended the log field, the compiler must subsequently look at it's usage, see the class org.slf4j.Logger which it does not recognise and throws an error. Under normal circumstances, due to the earlier compilation stage, the only possible cause is that the field doesn't exist, so infers that the symbol log must be missing. What it is really failing to understand is the type of the field log.

Because Lombok makes changes in the middle of compilation, I guess spurious errors such as these are always a possibility. Perhaps Lombok developers could fix it by doing their own check for org.slf4j.Logger. Most of the functionality provided by Lombok does not involve "importing" external classes, so I'm not surprised that it doesn't handle this edge case as elegantly as possible.

If you add the dependency for SLF4J, the compiler will no longer complain.

Otalgia answered 28/4, 2020 at 14:56 Comment(1)
Completely false. I have a working project, with all the imports and dependencies. When I try to build it locally on my machine, I get this error.Elvia
P
0

In lombok.config file for lombok.log.custom.declaration property use full name including the package for the classes

lombok.log.custom.declaration=com.mycomp.logging.Log
com.mycomp.logging.LogFactory.getLog(TYPE)(TOPIC)
Pulp answered 22/11, 2021 at 23:57 Comment(1)
There IS no lombok.config file. And no file with any of the strings your talking about.Elvia
G
0

If you already added Lombok plugin and enabled Lombok annotations, then your problem is on the IDE cache.

Invalidate Caches

enter image description here

Invalidate and Restart

enter image description here

Grapple answered 27/7, 2023 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.