NPE与Perf4j和Log4j

时间:2011-12-15 10:14:38

标签: java log4j perf4j

将Perf4j与Log4j一起使用时,我得到以下异常:

log4j:ERROR Error occured while converting date.
java.lang.NullPointerException
at java.lang.System.arraycopy(Native Method)
at java.lang.AbstractStringBuilder.getChars(AbstractStringBuilder.java:328)
at java.lang.StringBuffer.getChars(StringBuffer.java:201)
at org.apache.log4j.helpers.ISO8601DateFormat.format(ISO8601DateFormat.java:130)
at java.text.DateFormat.format(DateFormat.java:316)
at org.apache.log4j.helpers.PatternParser$DatePatternConverter.convert(PatternParser.java:444)
at org.apache.log4j.helpers.PatternConverter.format(PatternConverter.java:65)
at org.apache.log4j.PatternLayout.format(PatternLayout.java:502)
at org.apache.log4j.WriterAppender.subAppend(WriterAppender.java:302)
at org.apache.log4j.WriterAppender.append(WriterAppender.java:160)
at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251)
at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66)
at org.perf4j.log4j.AsyncCoalescingStatisticsAppender$1.handle(AsyncCoalescingStatisticsAppender.java:200)
at org.perf4j.helpers.GenericAsyncCoalescingStatisticsAppender$Dispatcher.run(GenericAsyncCoalescingStatisticsAppender.java:316)
at java.lang.Thread.run(Thread.java:662)

我的log4j.xml如下所示:

<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.out" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d %-5p: %c - %m%n" />
    </layout>
</appender>

<!-- Perf4j Appender -->
<appender name="CoalescingStatistics"
          class="org.perf4j.log4j.AsyncCoalescingStatisticsAppender">
    <param name="TimeSlice" value="10000"/>
    <appender-ref ref="fileAppender"/>
</appender>

<!-- This file appender is used to output aggregated performance statistics -->
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
    <param name="File" value="perfStats.log"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%m%n"/>
    </layout>
</appender>

<!-- Perf4j Loggers -->
<logger name="org.perf4j.TimingLogger" additivity="false">
    <level value="INFO"/>
    <appender-ref ref="CoalescingStatistics"/>
</logger>

我做错了什么?

1 个答案:

答案 0 :(得分:3)

我希望我可以帮助那些遇到同样异常的人。所以这是我的答案:

似乎所描述的例外仅在少数情况下出现。 这是我的设置:

  • 开发Web应用程序
  • Tomcat 6.0.32
  • Log4j 1.2.15
  • Perf4j 0.9.16

此外,似乎只有在重新部署Web应用程序后才会发生异常。如果您刚刚开始使用tomcat,则不会发生异常。

我做了什么来摆脱异常:

  • 将log4j模式从value="%d %-5p: %c - %m%n"更改为value="%d{yyyy-MM-dd HH:mm:ss} %-5p: %c - %m%n"
  • 将Log4j升级到版本1.2.16