当“slf4j-api”和“slf4j-simple”已经导入时,如何解决“找不到SLF4J提供者”?

pow*_*eed 3 java slf4j gradle

我在 gradle 项目中使用 Guice。运行main方法时,出现以下错误:

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.
Run Code Online (Sandbox Code Playgroud)

我做了一些研究,并在dependencies文件部分添加了两个依赖项build.gradle,如下所示:

dependencies {
    ...
    implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.0-alpha1'
    testImplementation group: 'org.slf4j', name: 'slf4j-simple', version: '2.0.0-alpha1'
}
Run Code Online (Sandbox Code Playgroud)

但错误仍然存​​在...

我需要通过 Guice 将 SLF4J 提供者与某些内容绑定吗?

我的main方法很简单,所以作为AbstractModuleGuice的类,如下所示(不确定它们是否相关):

public class Restful {
    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new ApplicationModule());
    }
}
Run Code Online (Sandbox Code Playgroud)

public class ApplicationModule extends AbstractModule {
    @Override
    protected void configure() {
        bind(UserDao.class).toInstance(new UserDao());
    }
}
Run Code Online (Sandbox Code Playgroud)

预先感谢!

Gaë*_*l J 9

slf4j-simple您仅在测试范围中添加,您也希望它在运行时范围中。

即在您的构建定义中更改为testImplementationimplementation


请注意,slf4j-simple顾名思义,这是 SLF4J API 的最小实现,您可能希望为生产代码使用更具可定制性的实现,例如 logback、log4j2...