Log4j Custom Appender最初抛出ClassNotFoundException?

时间:2013-06-28 13:00:44

标签: java log4j

我有以下log4j.xml文件,其中我使用的是Log4J Custom 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:&lt;%M&gt;:%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这样只为我的应用程序包加载它?

1 个答案:

答案 0 :(得分:1)

如上所述,您遇到了类加载器问题。

如果从父类加载器或webapp URLClassLoader加载slf4J + Log4J,很难从堆栈跟踪中猜出(更多需要)。

无论如何,如果slf4J和log4j安装在jetty /lib文件夹中(例如很可能由jetty父类加载器加载),任何自定义appender都应该位于同一/lib文件夹中的jar中。 / p>

或者,您可以尝试(未经测试):

  1. 以“标准”log4j配置文件(无自定义附加程序)
  2. 开头
  3. 在位于您的webapp的WEB-INF/lib文件夹中的jar中获取自定义appender的类,或者在WEB-INF/classes
  4. 中直接使用这些类
  5. 重写servlet的ìnit方法,以编程方式在Log 4j记录器上加载自定义appender。像

    这样的东西

    Logger.getLogger( "my.package" ).addAppender( new MyCustomAppender(blah) );

  6. 现在,针对“my.package”记录的servlet中的任何内容都应该定向到您的自定义appender。

    注意:SLF4J通常不能很好地应对在类加载器周围摆弄。