【问题标题】:Cannot use Slf4j with lombok无法将 Slf4j 与 lombok 一起使用
【发布时间】:2017-11-01 03:21:08
【问题描述】:

我正在尝试使用项目 lombok 来生成像示例 here 这样的记录器。

当我进入 intelliJ 时,我输入的 log 没有代码完成。我收到以下错误:

代码摘录:

package com.example.clement.recipeproject.bootstrap;

import com.example.clement.recipeproject.domain.*;
import com.example.clement.recipeproject.repositories.CategoryRepository;
import com.example.clement.recipeproject.repositories.RecipeRepository;
import com.example.clement.recipeproject.repositories.UnitOfMeasureRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Slf4j
@Component
public class DevBootstrap implements ApplicationListener<ContextRefreshedEvent> {

    private CategoryRepository categoryRepository;
    private UnitOfMeasureRepository unitOfMeasureRepository;
    private RecipeRepository recipeRepository;

    public DevBootstrap(CategoryRepository categoryRepository, UnitOfMeasureRepository unitOfMeasureRepository, RecipeRepository recipeRepository) {
        this.categoryRepository = categoryRepository;
        this.unitOfMeasureRepository = unitOfMeasureRepository;
        this.recipeRepository = recipeRepository;
    }

    // returns List<Recipe>
    private List<Recipe> getRecipes() {

        log.debug("I am a debug message");

*** 更新:

添加了更多照片以显示 slf4j 日志即将出现,但之后没有适当的方法。

当我跳入@Slf4j 时,我明白了。

注释处理器已打开,但不确定配置是否正确。

【问题讨论】:

  • 是的。我也尝试重新安装/重新启动intellij,但仍然是同样的问题:(
  • 您是否尝试过使缓存无效?
  • 不幸的是,清理缓存并重新启动后,同样的问题仍然存在。我在“更新”部分下添加了更多图片。
  • 如果您按住 Ctrl 键单击“日志”会发生什么?

标签: java intellij-idea slf4j lombok


【解决方案1】:

您是否将 slf4j 依赖项添加到 build.gradle 中,如下所示

dependencies {
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
}

【讨论】:

  • 是的,您需要添加实际的实现依赖,特别是当它不是 Spring 项目时。
【解决方案2】:

您确定从包 lombok.extern.slf4j.Slf4j 中导入了正确的 @Slf4 注解吗?

groovy.util.logging.Slf4j包中有同名注解。

【讨论】:

  • 是的,使用上面第 6 行的正确包import lombok.extern.slf4j.Slf4j;
猜你喜欢
  • 2019-05-20
  • 2021-02-18
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 2015-11-28
  • 1970-01-01
相关资源
最近更新 更多