Whats the root cause of Log4JLogger' cannot be found or is not useable?
我在类路径中有commons-logging.jar(v1.0.4)和log4j-1.2.8.jar,并遇到以下运行时错误:
1 2 3 4 5 6 | Caused by: org.apache.commons.logging.LogConfigurationException: User-specified log class 'org.apache.commons.logging.impl.Log4JLogger' cannot be found or is not useable. at org.apache.commons.logging.impl.LogFactoryImpl.discoverLogImplementation(LogFactoryImpl.java:874) at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:604) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:336) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:310) at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685) |
如果使用Maven,则必须确保在pom.xml中声明commons-logging和log4j。
1 2 3 4 5 6 7 8 9 10 11 12 | <dependencies> <dependency> <groupId>commons-logging</groupId> commons-logging</artifactId> <version>1.1.3</version> </dependency> <dependency> <groupId>log4j</groupId> log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> |
如果您仅声明公共信息,则出现错误
这是一个类路径问题,我们在类路径中确实有另一个版本的log4j。谢谢内森·瑞安(Nathan Ryan)!
使用log4j-2.x和commons-logging-1.2遇到此问题。恢复到log4j 1.2.x,一切正常。
这里有一个答案
使用log4j2
进行公共日志记录
包括库:
也可以看看:
https://logging.apache.org/log4j/2.x/faq.html#which_jars
当我尝试在eclipse 3.6的tomcat 7上运行Web应用程序时,有时会看到此错误。仅通过重新启动eclipse就会无处不在。
在服务器上多次(!)"清除所有项目"和Web应用程序的新部署之后,该应用程序将再次运行。也许其他人知道确切的"咒语"来解决这个奥秘而无需反复试验?
此错误似乎非常普遍。要获取更多详细信息,请使用以下选项启动JVM:
1 | -Dorg.apache.commons.logging.diagnostics.dest=STDOUT |
在我的情况下,发现一些缺少的依赖项。
1 2 3 4 5 6 7 8 9 10 11 12 | [LogFactoryImpl@929338653 from sun.misc.Launcher$AppClassLoader@1442407170] Attempting to instantiate 'org.apache.commons.logging.impl.Log4JLogger' [LogFactoryImpl@929338653 from sun.misc.Launcher$AppClassLoader@1442407170] Trying to load 'org.apache.commons.logging.impl.Log4JLogger' from classloader sun.misc.Launcher$AppClassLoader@1442407170 [LogFactoryImpl@929338653 from sun.misc.Launcher$AppClassLoader@1442407170] Class 'org.apache.commons.logging.impl.Log4JLogger' was found at 'jar:file:/home/mcadiz/NetBeansProjects/LogFilter/dist/lib/commons-logging-1.2.jar!/org/apache/commons/logging/impl/Log4JLogger.class' [LogFactoryImpl@929338653 from sun.misc.Launcher$AppClassLoader@1442407170] The log adapter 'org.apache.commons.logging.impl.Log4JLogger' is missing dependencies when loaded via classloader sun.misc.Launcher$AppClassLoader@1442407170: org/apache/log4j/Priority Exception in thread"main" java.lang.ExceptionInInitializerError Caused by: org.apache.commons.logging.LogConfigurationException: User-specified log class 'org.apache.commons.logging.impl.Log4JLogger' cannot be found or is not useable. at org.apache.commons.logging.impl.LogFactoryImpl.discoverLogImplementation(LogFactoryImpl.java:804) at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:541) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:292) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:269) at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:655) at com.oracle.logfilter.LogEntry.<clinit>(LogEntry.java:19) |
将log4j.jar添加到类路径中可以解决此问题。
当我的类路径中有apache commons日志jar时,却出现了此错误,但log4j jar却以某种方式丢失了。将log4j jar添加到类路径后,此错误就消失了。
我收到了同样的异常。
我确实有一个logger的实现。
它没有使用String作为参数的构造函数。
我实现了这样的构造函数,并为我解决了这个问题。
如果要为记录器提供实现,则该实现必须定义一个将java.lang.String作为参数的构造函数。否则,您将收到" org.apache.commons.logging.LogConfigurationException:无法找到或无法使用用户指定的日志类'...'。" Apache Wiki的常见问题页面上如此表示。
您是否正在创建任何特定的项目,例如Web服务或Spring等?如果是,那么请分析特定于项目的属性文件,该文件将具有引用Logger的属性,例如
如果您的Java项目带有servlet。
我遇到了这个问题,发现这是由于与log4j jar有关的类路径冲突引起的。
当我在项目中使用Maven时,解决方法是为log4j添加依赖项管理,如下所示。
1 2 3 4 5 6 7 8 9 10 11 | <dependencyManagement> <dependencies> <dependency> <groupId>log4j</groupId> log4j</artifactId> <version>1.2.17</version> <type>jar</type> <scope>compile</scope> </dependency> </dependencies> </dependencyManagement> |