我有以下 log4j.xml 文件,我在其中使用 Log4J 自定义 Appender
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender class="org.apache.log4j.RollingFileAppender" name="FILE">
<param value="${logfile}" name="File" />
<param value="10" name="MaxBackupIndex" />
<param value="200MB" name="MaxFileSize" />
<layout class="org.apache.log4j.PatternLayout">
<param value="%d[%t] %-5p(%F:<%M>:%L)- %m%n" name="ConversionPattern" />
</layout>
</appender>
<appender class="com.ubsc.at.properties.CustomAppender" name="CUSTAPPEN">
<layout class="org.apache.log4j.PatternLayout">
<param value="%m" name="ConversionPattern" />
</layout>
</appender>
<appender class="org.apache.log4j.AsyncAppender" name="ASYNC">
<param name="Blocking" value="false"/>
<param name="BufferSize" value="1000"/>
<appender-ref ref="CUSTAPPEN" />
</appender>
<root>
<level value="${logLevel}" />
<appender-ref ref="FILE" />
<appender-ref ref="ASYNC" />
</root>
</log4j:configuration>
当应用程序启动时,我在码头控制台中收到以下异常
java.lang.ClassNotFoundException: com.ubsc.at.properties.CustomAppender
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.apache.log4j.helpers.Loader.loadClass(Loader.java:198)
at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:247)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:176)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.java:191)
at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:284)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:176)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.java:191)
at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurator.java:523)
at org.apache.log4j.xml.DOMConfigurator.parseRoot(DOMConfigurator.java:492)
at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:1001)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:867)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:773)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:483)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:73)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
at org.eclipse.jetty.util.log.Slf4jLog.<init>(Slf4jLog.java:44)
at org.eclipse.jetty.util.log.Slf4jLog.<init>(Slf4jLog.java:27)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
我看到问题是 log4j 在我的类加载之前被初始化。
可以做些什么来避免这个错误?复制现有 log4j.jar 中的类文件并创建一个新 jar?
请分享你的想法。有什么方法可以设置我的 CustomAppender 只为我的应用程序包加载它?