📜  SLF4J-错误消息

📅  最后修改于: 2020-11-11 05:12:17             🧑  作者: Mango


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

无法加载类“ org.slf4j.impl.StaticLoggerBinder”。

这是在类路径中未提供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.

要解决此问题,您需要添加任何日志记录框架绑定。本教程的“ Hello world”一章对此进行了说明。

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

找不到SLF4J提供程序

在slf4j-1.8.0-beta2中,上面的警告更清楚地表明“未找到SLF4J提供程序”

以下是完整的警告-

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.

类路径包含针对1.8之前的slf4j-api版本的SLF4J绑定

如果您使用的是SLF4J 1.8版本,并且在类路径中具有以前版本的绑定,但没有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:/C:/Users/Tutorialspoint/Desktop/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,并且您的类路径中只有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添加到您的类路径中。

在类路径上检测到jcl-over-slf4j.jar和绑定的slf4j-jcl.jar。

绑定slf4j-jcl.jar将slf4j记录器的调用重定向到JCL,并且jcl-over-slf4j.jar将JCL记录器的调用重定向到slf4j。因此,您不能在项目的类路径中同时使用它们。如果这样做,您将得到如下异常。

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.(StaticLoggerBinder.java:71)
   at org.slf4j.impl.StaticLoggerBinder.(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.(JCLLoggerFactory.java:54)
   ... 7 more

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

检测到记录器名称不匹配

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

  • 将要创建的记录器的名称作为参数传递给getLogger()方法。

  • 将类作为参数传递给此方法。

如果尝试通过将类作为参数传递来创建记录器工厂对象,并且将系统属性slf4j.detectLoggerNameMismatch设置为true,则将类的名称作为参数传递给getLogger()方法,并您使用的类应该相同,否则您将收到以下警告:

“检测到的记录器名称不匹配。

考虑以下示例。

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

      //Logging the information
      logger.info("Hi Welcome to Tutorilspoint");
   }
}

在这里,我们将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 Welcome to Tutorilspoint

注意-这在slf4j 1.7.9之后发生

类路径包含多个SLF4J绑定。

您在类路径中应该只有一个绑定。如果您有多个绑定,则会收到一条警告,列出绑定及其位置。

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

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in
[jar:file:/C:/Users/Tutorialspoint/Desktop/Latest%20Tutorials/SLF4J%20Tutorial/
slf4j-1.7.25/slf4j-nop-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in
[jar:file:/C:/Users/Tutorialspoint/Desktop/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]

在类路径上检测到log4j-over-slf4j.jar和绑定的slf4j-log4j12.jar

要将log4j记录程序调用重定向到slf4j,您需要使用log4j-over-slf4j.jar绑定,如果要将slf4j调用重定向到log4j,则需要使用slf4j-log4j12.jar绑定。

因此,您不能在类路径中同时使用它们。如果这样做,您将得到以下异常。

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.(StaticLoggerBinder.java:72)
   at org.slf4j.impl.StaticLoggerBinder.(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.