简体   繁体   English

在通过 Lombok 的“@Slf4j”注释创建的“log”object 中找不到 error()、debug()、info() 方法

[英]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?有人可以帮助我在 Lombok 中使用 @Slf4j 注释吗?

I prefer to log something in my SpringBoot app.我更喜欢在我的 SpringBoot 应用程序中记录一些东西。 For this I want to use @Slf4j annotation from Lombok.为此,我想使用 Lombok 的 @Slf4j 注释。 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.我在我的 IntelliJ IDEA 中安装了 Lombok 插件,打开“注释处理”,将 @Slf4j 注释添加到我的 class 并将 lombok 依赖项添加到我的 pom.xml 文件中。 Now I can find object 'log' in the class, but I can't apply any log methods to it (like error(), debug(), info(), etc.).现在我可以在 class 中找到 object 的“日志”,但我无法对其应用任何日志方法(如 error()、debug()、info() 等)。 Why aren't these methods found?为什么找不到这些方法?

IntelliJ IDEA Community 2020.3 IntelliJ IDEA 社区 2020.3
Lombok plugin bundled 203.7717.56 Lombok 插件捆绑 203.7717.56
lombok 1.18.6龙目岛 1.18.6

dependency in pom.xml: 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:登录我的 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. Lombok 本身并没有带来任何关于日志框架等的依赖项。您需要自己添加日志框架依赖项。

Eg in your case, add the following dependency, it will bring SLF4J as well as Logback as the logging implementation:例如,在您的情况下,添加以下依赖项,它将带来 SLF4J 以及 Logback 作为日志记录实现:

<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.只是 IDE 在抱怨。

In my case I had to Invalidate Caches.就我而言,我必须使缓存无效。

Go to File > Invalidate Caches (select "Clear file system caches and local history") > Invalidate and Restart. Go 到 File > Invalidate Caches(选择“Clear file system caches and local history”)> Invalidate and Restart。

Hope that works.希望有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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