java.lang.ClassCastException - Log4j

时间:2016-05-27 01:47:41

标签: java log4j weblogic classloader apache-commons-logging

启动托管服务器时,我在WebLogic 12c托管服务器输出文件中看到与log4j相关的以下ClassCastException。我在我的WAR的lib目录中捆绑了commons-logging-1.1.1.jar和log4j-1.2.17.jar,那里没有其他版本的库。我还粘贴了处理请求时生成的托管服务器日志文件错误。我应该使用WebLogic 12c附带的log4j和commons日志记录吗?我在modules目录中看到了这些:com.bea.core.apache.commons.logging_1.1.2.jar和com.bea.core.apache.log4j_1.2.0.0_1-2-15.jar。我之前没有遇到过log4j和WebLogic的问题。

out file:

jadomain.lang.ClassCastException: org.apache.log4j.RollingFileAppender cannot be cast to org.apache.log4j.Appender
        at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.jadomain:248)
        at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.jadomain:176)
        at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.jadomain:191)
        at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurator.jadomain:523)
        at org.apache.log4j.xml.DOMConfigurator.parseCategory(DOMConfigurator.jadomain:436)
        at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.jadomain:1004)
        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.jadomain:872)
        at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.jadomain:778)
        at org.apache.log4j.xml.DOMConfigurator.configure(DOMConfigurator.jadomain:906)
        at com.domain.d.app.restwrapper.ContextListener.contextInitialized(ContextListener.jadomain:37)
        at weblogic.servlet.internal.EventsManager$FireContextListenerAction.run(EventsManager.jadomain:66

日志文件:

]] Root cause of ServletException.
jadomain.lang.LinkageError: loader constraint violation: when resolving method "org.apache.log4j.LogMF.entering(Lorg/apache/log4j/Logger;Ljadomain/lang/String;Ljadomain/lang/String;)V" the class loader (instance of weblogic/utils/classloaders/ChangeAwareClassLoader) of the current class, com/domain/d/app/restwrapper/appResource, and the class loader (instance of weblogic/utils/classloaders/GenericClassLoader) for resolved class, org/apache/log4j/LogMF, have different Class objects for the type /lang/String;)V used in the signature
        at com.domain.d.app.restwrapper.appResource.addQuery(appResource.jadomain:244)
        at com.domain.d.app.restwrapper.appResource.addQuery(appResource.jadomain:224)
        at com.domain.d.app.restwrapper.appResource$Proxy$_$$_WeldClientProxy.addQuery(appResource$Proxy$_$$_WeldClientProxy.jadomain)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jadomain:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jadomain:43)

1 个答案:

答案 0 :(得分:1)

两个版本的log4j之间存在冲突。一个从服务器的类路径加载,另一个从Web应用程序的类路径加载。 要解决此问题,您可以将Web应用程序设置为仅使用应用程序中的应用程序。更新您的weblogic.xml文件并添加以下标记:

<prefer-web-inf-classes>true</prefer-web-inf-classes>