java.lang.ClassCastException - Log4j

java.lang.ClassCastException - Log4j

启动托管服务器时,我在 WebLogic 12c 托管服务器输出文件中看到以下与 log4j 相关的 ClassCastException。我有 commons-logging-1.1.1.jar 和 log4j-1.2.17.jar 捆绑在我的 WAR 的 lib 目录中,那里没有这些库的其他版本。我还粘贴了在处理请求时生成的托管服务器日志文件错误。我应该使用 WebLogic 12c 附带的 log4j 和公共日志记录吗?我在模块目录中看到这些: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 的问题。

输出文件:

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)

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

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