java 使用自定义类加载器配置 org.apache.log4j.ConsoleAppender

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12404934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:45:50  来源:igfitidea点击:

Configure org.apache.log4j.ConsoleAppender with custom classloader

javalog4jclassloaderjavassist

提问by Avner Levy

I have a java class which creates a custom classloader based on javassist class loader on start up and then run the real program class. I'm getting the following error:

我有一个 java 类,它在启动时创建一个基于 javassist 类加载器的自定义类加载器,然后运行真正的程序类。我收到以下错误:

log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to a        
"org.apache.log4j.Appender" variable.
log4j:ERROR The class "org.apache.log4j.Appender" was loaded by 
log4j:ERROR [javassist.Loader@6f97b10a] whereas object of type 
log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by 
[java.net.URLClassLoader@5b414a8d].
log4j:ERROR Could not instantiate appender named "stdout".

The problem is related to the fact that one object is created by the original classloader while the other is created by the custom one.
Is there a way to resolve this error?

问题与这样一个事实有关,即一个对象是由原始类加载器创建的,而另一个是由自定义类加载器创建的。
有没有办法解决这个错误?

Thanks in advance,
Avner

提前致谢,
Avner

回答by Sola Yang

Please try to set -Dlog4j.ignoreTCL=true, hope it helps. a simular issue about log4j

请尝试设置 -Dlog4j.ignoreTCL=true,希望对您有所帮助。关于 log4j 的一个类似问题

回答by Tharinda

Add log4j.ignoreTCLin maven tomcat plugin configuration as shown in below

在maven tomcat插件配置中添加log4j.ignoreTCL如下图

<plugin>
   <groupId>org.apache.tomcat.maven</groupId>
   <artifactId>tomcat7-maven-plugin</artifactId>
   <version>2.1</version>
   <configuration>
      <port>${local.server.port}</port>
      <update>true</update>
      <systemProperties>
         <log4j.configurationFile>${user.dir}\conf\log4j2.xml</log4j.configurationFile>
         <log4j.ignoreTCL>true</log4j.ignoreTCL>
         <java.util.logging.manager>org.apache.logging.log4j.jul.LogManager</java.util.logging.manager>
      </systemProperties>
      <contextReloadable>true</contextReloadable>
   </configuration>
</plugin>