NPE with Perf4j and Log4j

2019-08-05 04:41发布

问题:

while using Perf4j together with Log4j I get the following exception:

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)

My log4j.xml looks like this:

<!-- 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>

What am I doing wrong?

回答1:

I hope I can help someone who just ran into the same exception. So here is my answer:

It seems that the described exception only appears under a few circumstances. Here is my setup:

  • Developing a web application
  • Tomcat 6.0.32
  • Log4j 1.2.15
  • Perf4j 0.9.16

Furthermore it seems that the exception only occurs after one redeploys the web application. The exception does NOT occur if you just fresh start the tomcat.

What I have done to get rid of the exception:

  • Changed log4j pattern from value="%d %-5p: %c - %m%n" to value="%d{yyyy-MM-dd HH:mm:ss} %-5p: %c - %m%n"
  • Upgraded Log4j to version 1.2.16