教程 > SLF4J 教程 > SLF4J 基础 阅读:282

SLF4J 错误消息

在本章中,我们将讨论在使用 SLF4J 时收到的各种错误消息或警告以及这些消息的原因/含义。

Failed to load class "org.slf4j.impl.StaticLoggerBinder".

这是在 CLASSPATH 中没有提供 SLF4J 绑定时引起的警告。

以下是完整的警告消息

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

要解决此问题,我们需要添加任一日志框架绑定。 这在本教程的 SLF4J 示例章节中进行了解释。

注意 - 这发生在 1.6.0 和 1.8.0-beta2 之间的 SLF4J 版本中。

No SLF4J providers were found

slf4j-1.8.0-beta2 中,上面的警告更清楚地说“No SLF4J providers were found”。

以下是完整的警告

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.

Classpath contains SLF4J bindings targeting slf4j-api versions prior to 1.8

如果我们使用的是 SLF4J 1.8 版本,并且 CLASSPATH 中有以前版本的绑定,但没有 1.8 的绑定,我们将看到如下所示的警告。

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.
SLF4J: Class path contains SLF4J bindings targeting slf4j-api versions prior to
1.8.
SLF4J: Ignoring binding found at
[jar:file:/workspace/Java/Latest%20Tutorials/SLF4J%20Tutorial/
slf4j-1.7.25/slf4j-jdk14-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#ignoredBindings for an explanation.

NoClassDefFoundError: org/apache/commons/logging/LogFactory

如果我们正在使用 slf4j-jcl 并且如果 CLASSPATH 中只有 slf4j-jcl.jar,我们将收到如下所示的异常。

Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/logging/LogFactory
at org.slf4j.impl.JCLLoggerFactory.getLogger(JCLLoggerFactory.java:77)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:358)
at SLF4JExample.main(SLF4JExample.java:8)
Caused by: java.lang.ClassNotFoundException:
org.apache.commons.logging.LogFactory
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more

要解决此问题,我们需要将 commons-logging.jar 添加到 CLASSPATH 中。

Detected both jcl-over-slf4j.jar AND bound slf4j-jcl.jar on the classpath..

绑定 slf4j-jcl.jar 将 slf4j Logger 的调用重定向到 JCL,而 jcl-over-slf4j.jar 将 JCL Logger 的调用重定向到 slf4j。 因此,我们不能在项目的 CLASSPATH 中同时拥有这两者。 如果这样做,我们会得到一个异常,如下所示。

SLF4J: Detected both jcl-over-slf4j.jar AND bound slf4j-jcl.jar on the class
path, preempting StackOverflowError.
SLF4J: See also http://www.slf4j.org/codes.html#jclDelegationLoop for more
details.
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:71)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:42)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
at SLF4JExample.main(SLF4JExample.java:8)
Caused by: java.lang.IllegalStateException: Detected both jcl-over-slf4j.jar
AND bound slf4j-jcl.jar on the class path, preempting StackOverflowError. See
also http://www.slf4j.org/codes.html#jclDelegationLoop for more details.
at org.slf4j.impl.JCLLoggerFactory.<clinit>(JCLLoggerFactory.java:54)
... 7 more

要解决此问题,请删除其中一个 jar 文件。

Detected logger name mismatch

我们可以通过以下方式创建 Logger 对象:

  • 将要创建的 Logger 的名称作为参数传递给 getLogger() 方法。
  • 将类作为参数传递给此方法。

如果我们尝试通过将类作为参数传递来创建 Logger Factory 对象,并且我们已将系统属性 slf4j.detectLoggerNameMismatch 设置为 true,那么我们作为参数传递给 getLogger() 方法的类的名称和我们使用的类名应该相同,否则我们将收到以下警告 -

“Detected logger name mismatch”

考虑以下示例。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SLF4JExample {
   public static void main(String[] args) {
      System.setProperty("slf4j.detectLoggerNameMismatch", "true");
      
      // 创建 Logger 对象
      Logger logger = LoggerFactory.getLogger(Sample.class);

      // 记录信息
      logger.info("Hi 欢迎来到 迹忆客");
   }
}

在这里,我们将 slf4j.detectLoggerNameMismatch 属性设置为 true。 我们使用的类名是 SLF4JExample,我们传递给 getLogger() 方法的类名是 Sample,因为它们都不相等,我们将收到以下警告。

SLF4J: Detected logger name mismatch. Given name: "Sample"; computed name:
"SLF4JExample".
SLF4J: See http://www.slf4j.org/codes.html#loggerNameMismatch for an
explanation
Dec 10, 2018 12:43:00 PM SLF4JExample main
INFO: Hi 欢迎来到 迹忆客

注意 - 这发生在 slf4j 1.7.9 之后

Classpath contains multiple SLF4J bindings.

我们应该在 CLASSPATH 中只有一个绑定。 如果我们有多个绑定,将收到列出绑定及其位置的警告。

假设,如果我们在 CLASSPATH 中有绑定 slf4j-jdk14.jarslf4j-nop.jar,我们将收到以下警告。

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in
[jar:/worksapce/java/Latest%20Tutorials/SLF4J%20Tutorial/
slf4j-1.7.25/slf4j-nop-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in
[jar:/worksapce/java/Latest%20Tutorials/SLF4J%20Tutorial/
slf4j-1.7.25/slf4j-jdk14-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an
explanation.
SLF4J: Actual binding is of type [org.slf4j.helpers.NOPLoggerFactory]

Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path

要将 log4j Logger 调用重定向到 slf4j,我们需要使用 log4j-over-slf4j.jar 绑定,如果要将 slf4j 调用重定向到 log4j,则需要使用 slf4j-log4j12.jar 绑定。

因此,我们不能在 CLASSPATH 中同时拥有这两者。 如果这样做,我们将收到以下异常。

SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the
class path, preempting StackOverflowError.
SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more
details.
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72)
at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:45)
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383)
at SLF4JExample.main(SLF4JExample.java:8)
Caused by: java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar
AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError.
See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.

查看笔记

扫码一下
查看教程更方便