title: Performance of log4j2 compared to log4j1
body: <p>I'm trying to migrate my app into using log4j2. It is currently using log4j 1.2.16. I also have a performance build for my project, and after upgrading to log4j 2, the performance seemed to have improve a lot. </p>
  
  <p>That is, until I read about bridging. According to the doc, I have to exclude log4j1 JAR from the classpath, and include the bridging JAR - which I assume is named 'org.apache.logging.log4j:log4j-1.2-api'. Once I did this, performance dropped again.</p>
  
  <p>So to summarise:</p>
  
  <ol>
  <li>Performance with log4j2 + bridging jar + log4j-1.2-api + log4j1 : good</li>
  <li>Performance with log4j2 + bridging jar + log4j-1.2-api : bad (to the point that it drops back to performance of just log4j1)</li>
  </ol>
  
  <p>I have checked that the log4j-1.2-api is earlier in the classpath. So it should have been loaded first. </p>
  
  <p>Any idea what could cause this problem? </p>
  
  <p>Thank you very much in advance!</p>
  
  <p>Oh my complete classpath for logging are:</p>
  
  <ul>
  <li>org.slf4j:slf4j-api</li>
  <li>org.slf4j:log4j-over-slf4j</li>
  <li>org.slf4j:jcl-over-slf4j</li>
  <li>org.apache.logging.log4j:log4j-slf4j-impl</li>
  <li>org.apache.logging.log4j:log4j-core</li>
  <li>org.apache.logging.log4j:log4j-api</li>
  <li>org.apache.logging.log4j:log4j-1.2-api</li>
  <li>log4j:log4j  (with &amp; without, as described above)</li>
  </ul>
  
  <p>Versions:</p>
  
  <ul>
  <li>Log4j2 : 2.6.2</li>
  <li>slf4j: 1.7.20</li>
  <li>log4j1: 1.2.16</li>
  </ul>
  
  <p>My config file looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Configuration
          xmlns:xi="http://www.w3.org/2001/XInclude"&gt;
  
      &lt;xi:include href="log4j2-xinclude-appenders.xml" /&gt;
  
      &lt;Loggers&gt;
  
          &lt;Root level="info"&gt;
              &lt;AppenderRef ref="rollingFileAppender"/&gt;
              &lt;AppenderRef ref="stdOutAppender"/&gt;
          &lt;/Root&gt;
      &lt;/Loggers&gt;
  &lt;/Configuration&gt;
  </code></pre>
  
  <p>And the log4j2-xinclude-appenders.xml looks like:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;appenders&gt;
      &lt;RollingRandomAccessFile name="_rollingFileAppender" fileName="./logs/foo-${sys:app.name.suffix}.log"
                   filePattern="./logs/foo-${sys:foo.app.name.suffix}.log.%i"&gt;
          &lt;PatternLayout&gt;
              &lt;Pattern&gt;%d|%X{active.profiles}| %-5p |%X{fcp.session}|%X{StateMachine.key}|%X{StateMachine.currentState}| %m | %t | %c{1.}%n&lt;/Pattern&gt;
          &lt;/PatternLayout&gt;
          &lt;Policies&gt;
              &lt;OnStartupTriggeringPolicy minSize="0" /&gt;
              &lt;SizeBasedTriggeringPolicy size="100 MB" /&gt;
          &lt;/Policies&gt;
          &lt;DefaultRolloverStrategy max="10"/&gt;
      &lt;/RollingRandomAccessFile&gt;
  
      &lt;Async name="rollingFileAppender" blocking="false" bufferSize="10000"&gt;
          &lt;AppenderRef ref="_rollingFileAppender"/&gt;
      &lt;/Async&gt;
  
      &lt;Console  name="_stdOutAppender" target="SYSTEM_OUT"&gt;
          &lt;PatternLayout pattern="%d|%X{active.profiles}| %-5p |%X{fcp.session}|%X{StateMachine.key}|%X{StateMachine.currentState}| %m | %t | %c{1.}%n"/&gt;
      &lt;/Console&gt;     
      &lt;Async name="stdOutAppender" blocking="false" bufferSize="10000"&gt;
          &lt;AppenderRef ref="_stdOutAppender"/&gt;
      &lt;/Async&gt;
  &lt;/appenders&gt;
  </code></pre>
  
  <p><strong>EDIT:</strong> This is the log4j 1 xml file that gets included in the classpath</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt;
  
  &lt;log4j:configuration debug="false" xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;
  
      &lt;appender name="A1" class="org.apache.log4j.ConsoleAppender"&gt;
          &lt;layout class="org.apache.log4j.PatternLayout"&gt;
              &lt;param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/&gt;
          &lt;/layout&gt;
      &lt;/appender&gt;
  
      &lt;appender name="R" class="com.bar.common.util.RollingFileAppender"&gt;
          &lt;param name="File" value="./logs/bar.log"/&gt;
          &lt;param name="MaxFileSize" value="100MB"/&gt;
          &lt;param name="MaxBackupIndex" value="10"/&gt;
          &lt;layout class="org.apache.log4j.PatternLayout"&gt;
              &lt;param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/&gt;
          &lt;/layout&gt;
      &lt;/appender&gt;
  
      &lt;!-- Performance Appender --&gt;
      &lt;appender name="OneSecondStatsAppender"
                class="com.foo.perf.AggregatedStatisticsAppender"&gt;
          &lt;param name="TimeSlice" value="1000"/&gt;
          &lt;appender-ref ref="OneSecondStatsLogger"/&gt;
      &lt;/appender&gt;
      &lt;appender name="FiveMinuteStatsAppender"
                class="com.bar.perf.DatafabricAggregatedStatisticsAppender"&gt;
          &lt;param name="TimeSlice" value="300000"/&gt;
          &lt;appender-ref ref="FiveMinuteStatsLogger"/&gt;
      &lt;/appender&gt;
  
      &lt;!-- Aggregated Performance Statistics Appender --&gt;
      &lt;appender name="OneSecondStatsLogger" class="org.apache.log4j.FileAppender"&gt;
          &lt;param name="File" value="./logs/bar-performance.log"/&gt;
          &lt;layout class="com.bar.perf.AggregatedStatisticsCsvLayout"/&gt;
          &lt;filter class="com.bar.perf.CategorisedStatisticExclusionFilter"/&gt;
      &lt;/appender&gt;
      &lt;appender name="FiveMinuteStatsLogger" class="org.apache.log4j.FileAppender"&gt;
          &lt;param name="File" value="./logs/bar-minutes.log"/&gt;
          &lt;layout class="com.bar.perf.AggregatedStatisticsCsvLayout"&gt;
              &lt;param name="ShowEmptyStatistics" value="true"/&gt;
          &lt;/layout&gt;
      &lt;/appender&gt;
  
      &lt;!-- Loggers --&gt;
      &lt;logger name="org.perf4j.TimingLogger" additivity="false"&gt;
          &lt;level value="INFO"/&gt;
          &lt;appender-ref ref="OneSecondStatsAppender"/&gt;
          &lt;appender-ref ref="FiveMinuteStatsAppender"/&gt;
      &lt;/logger&gt;
  
      &lt;logger name="com.bar"&gt;
          &lt;level value="INFO"/&gt;
      &lt;/logger&gt;
  
      &lt;logger name="com.gemstone.gemfire"&gt;
          &lt;level value="INFO"/&gt;
      &lt;/logger&gt;
  
      &lt;logger name="org.springframework.data"&gt;
          &lt;level value="INFO"/&gt;
      &lt;/logger&gt;
  
      &lt;!-- Root logger configuration --&gt;
      &lt;root&gt;
          &lt;priority value="INFO"/&gt;
          &lt;appender-ref ref="R"/&gt;
      &lt;/root&gt;
  
  &lt;/log4j:configuration&gt;
  </code></pre>
  
  <p><strong>EDIT 2</strong>:
  Classpath order for poor performance:</p>
  
  <pre><code>log4j-1.2-api-2.6.2.jar
  jcl-over-slf4j-1.7.20.jar
  slf4j-api-1.7.20.jar
  log4j-slf4j-impl-2.6.2.jar
  log4j-core-2.6.2.jar
  log4j-api-2.6.2.jar
  log4j-1.2.16.jar
  </code></pre>
  
  <p>Classpath order for good performance</p>
  
  <pre><code>log4j-1.2-api-2.6.2.jar
  jcl-over-slf4j-1.7.20.jar
  slf4j-api-1.7.20.jar
  log4j-over-slf4j-1.7.20.jar
  log4j-slf4j-impl-2.6.2.jar
  log4j-core-2.6.2.jar
  log4j-api-2.6.2.jar
  log4j-1.2.16.jar
  </code></pre>