Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding lombok dependency and @Slf4j does not let use logger

Tags:

java

lombok

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>
like image 747
makiz123 Avatar asked Jul 19 '19 13:07

makiz123


People also ask

What does @SLF4J annotation do?

Annotation Type Slf4jCauses lombok to generate a logger field. Complete documentation is found at the project lombok features page for lombok log annotations. This annotation is valid for classes and enumerations.

What does Lombok use for logging?

Lombok's @Slf4j generates a logger instance with default name log using the SLF4J API. Note that we must also include a library that implements the Slf4j API.


2 Answers

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>
like image 131
rzwitserloot Avatar answered Oct 06 '22 01:10

rzwitserloot


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'
    ...
}
like image 29
Greg Avatar answered Oct 05 '22 23:10

Greg