简体   繁体   中英

Can't find error(), debug(), info() methods in 'log' object which is created via Lombok's '@Slf4j' annotation

Can anybody help me with @Slf4j annotation in Lombok?

I prefer to log something in my SpringBoot app. For this I want to use @Slf4j annotation from Lombok. I installed Lombok plugin in my IntelliJ IDEA, turn on 'Annotation processing', add @Slf4j annotation to my class and add lombok dependency to my pom.xml file. Now I can find object 'log' in the class, but I can't apply any log methods to it (like error(), debug(), info(), etc.). Why aren't these methods found?

IntelliJ IDEA Community 2020.3
Lombok plugin bundled 203.7717.56
lombok 1.18.6

dependency in pom.xml:

<properties>
    <lombok.version>1.18.6</lombok.version>
</properties>

<dependencies>
     <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <version>${lombok.version}</version>
     </dependency>
</dependencies>

logging in my class:

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
@Service
public class ImportServiceImpl implements ImportService {
   ...
   public void importData() {
      log.info("some log");
   }
   ...
}

Lombok itself does not bring any dependencies regarding log frameworks etc. You need to add the log framework dependencies yourself.

Eg in your case, add the following dependency, it will bring SLF4J as well as Logback as the logging implementation:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
</dependency>

I had everything in place. The build was working and I was able to run the application without any compilation error. Just that the IDE was complaining.

In my case I had to Invalidate Caches.

Go to File > Invalidate Caches (select "Clear file system caches and local history") > Invalidate and Restart.

Hope that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM