为什么当SLF4J在Maven pom.xml文件中时,"No SLF4J"提供程序会发现错误?



我目前在尝试运行Spring Java程序时得到以下错误:

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions 1.7.x or earlier.
SLF4J: Ignoring binding found at [jar:file:/C:/Users/user/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Ignoring binding found at [jar:file:/C:/Users/user/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.19.0/log4j-slf4j-impl-2.19.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See https://www.slf4j.org/codes.html#ignoredBindings for an explanation.

下面是pom.xml文件中包含SLF4J和Log4J依赖项的部分。

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.19.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.19.0</version>
</dependency>

我没有任何针对slf4j-api版本1的指示的组件。X或更早。这个错误消息似乎既说我没有SLF4J,又说我有错误的版本。

如果有任何关于如何解决这个问题的想法,我将不胜感激。谢谢你!

我尝试在pom.xml文件中使用不同的依赖项,但最终这些都在以前的程序中工作。我还尝试查看可能也使用SLF4J的依赖项,并从中排除SLF4J,但我仍然得到错误消息。我已经尝试了其他类似问题的解决方案,但总是得到同样的错误。

基于此文档:

由于SLF4J绑定的兼容性中断,从2.19.0版本开始提供了两个SLF4J到Log4j的适配器。

  1. log4j-slf4j-impl应与SLF4J 1.7一起使用。
  2. log4j-slf4j2-impl应该与SLF4J 2.0一起使用。

利用Java模块系统的应用程序应该使用SLF4J 2.0。log4j-slf4j2-impl.

所以最简单的方法(如果你需要SLF4J 2)是使用<artifactId>log4j-slf4j2-impl</artifactId>。您可以做的另一件事是恢复到slf4j-api1.7.x。

其他笔记:

  • SLF4J日志显示logback-classic也存在于依赖树中。您需要选择logback-classic或log4j绑定。在你的项目上运行mvn dependency:tree,找到logback-classic被添加的位置。

  • 如果您想保留logback-classic,那么就像上面提到的那样,您需要使用与SLF4J 2兼容的logback-classic

我使用Gradle。添加此依赖项有效

implementation 'org.apache.logging.log4j:log4j-slf4j2-impl:2.20.0'