我試圖建立與SLF4J + log4j的一個項目,但該死的東西只是不工作...我不斷收到異常:如何將SLF4J與Log4J綁定?
Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: org/apache/log4j/Level
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:129)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:302)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:276)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:288)
at TestLog.main(TestLog.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Level
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:129)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:302)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:276)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:288)
at TestLog.main(TestLog.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
我檢查和類org.apache.log4j.Level在我的項目「Maven Dependencies> log4j-1.2.17.jar」中有。那麼爲什麼虛擬機找不到它?
我嘗試了下面的選項已經:
- SLF4J的API + log4j的
- SLF4J-API + SLF4J-log4j12
- SLF4J的API + log4j的
- SLF4J-API + SLF4J-log4j12 + log4j的
的選項都不工作:(
這是我currnet的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.sample</groupId>
<artifactId>DBUnitSample</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>
另外,我注意到,增加SLF4J-log4j12時,在樹中添加的log4j作爲依賴一樣,所以我猜測,這裏明確加入log4j的是多餘的,但我已經添加了它。仍然沒有運氣。
我主要的代碼非常簡單:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SampleDao {
private static final Logger log = LoggerFactory.getLogger(SampleDao.class);
public static void main(String[] args) {
System.out.println("Hello");
log.info("Logged");
}
}
我的log4j.properties:
log4j.rootLogger=DEBUG, STDOUT
log4j.logger.deng=INFO
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
有人能告訴我什麼我做錯了?我已經試過在SO上尋找答案,但是所有提到的添加log4j依賴項的答案,我已經添加了......所以不確定,最新的錯誤。
編輯:這裏是我的整個項目結構。我R-clicking SampleDao
並將其作爲Java應用程序運行。 (我也刪除DbUnit的依賴,現在,以零上的SLF4J問題)
工作對我來說,也許你的問題是,當您運行您的程序 – morgano 2014-09-06 21:03:14
你怎麼跑你的程序不指定在classpath的罐子?嘗試添加一些明確的調用log4j,如果這有效,原因將是你的建設工具。 – Loki 2014-09-07 03:57:21
我在Eclipse中使用嵌入式maven,並將其創建爲maven項目。所以我使用Eclipse Run Configurations來運行類作爲Java應用程序(R-click> Run as> Java Application)。我也在運行配置中檢查了類路徑,我可以看到Maven Dependencies是其中的一部分。所以不知道爲什麼log4j沒有被發現。 – Pat 2014-09-07 08:04:05