
SLF4J - 錯誤資訊
本章將討論使用 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章節對此進行了說明。
注意 - 這發生在 SLF4J 的 1.6.0 到 1.8.0-beta2 之間的版本中。
未找到任何 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 和 bound 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.<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 檔案。
檢測到日誌器名稱不匹配
您可以透過以下方式建立 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.jar和slf4j-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 和 bound 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.<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.