7

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
Michael
  • 41,989
  • 11
  • 82
  • 128
kaiffeetasse
  • 481
  • 1
  • 8
  • 18

4 Answers4

6

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

Michael
  • 41,989
  • 11
  • 82
  • 128
Roberto Manfreda
  • 2,345
  • 3
  • 25
  • 39
  • 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. –  Sep 26 '22 at 14:28
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.

Michael
  • 41,989
  • 11
  • 82
  • 128
  • 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. –  Sep 26 '22 at 14:29
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)
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
  • There IS no lombok.config file. And no file with any of the strings your talking about. –  Sep 26 '22 at 14:30
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

Gayan Mettananda
  • 1,498
  • 14
  • 21