Log4j2 vs Log4j

Log4j2 is the evolution not only to Log4j but also to Logback, as it takes Logback’s feature one step forward.

The main selling point is the improved performance, throughput of messages and latency, which apparently is a huge leap forward compared to Log4j and also Logback.

Other interesting Log4j2 features are:

  • Automatic reloading of logging configurations
  • Property Support: Log4j2 loads the system’s properties and they can be evaluated even at the configuration level
  • Java8 lambdas and lazy evaluation: It provides an API for wrapping a log message inside a lambda statement, which only gets evaluated if truly needed
  • Garbage free: An interesting architectural feature, as Log4j2 has no or very little (in case of web apps) garbage. You can read more about that here.
  • Async loggers using the LMAX Disruptor: The disruptor is a very interesting technology and it is always provoking to examine use cases of it being used in strain

I played around with Log4j2 and in general i was very happy with its API, implementation ( it actually separates the API from the implementation, even though that means the developer needs to add 2 maven dependencies), configuration simplicity and finally the performance.

Even though measuring a logger’s performance with JMH is not advisable i tried to compare its performance (using async and sync loggers) against the old Log4j. The performance (average time and throughput) was indeed better and at the edge cases 15K ops/ms faster!. Having said that, you should take that with a pinch of salt, because as mentioned earlier JMH is not the right tool to performance measure and compare the two logging implementation.

For reference the simple java program used to perform the various tests can be found in Github.

Some indicative results, performing 3 runs for each logger can be seen below.

Log4j2 Async Logger:
#1
Benchmark Mode Cnt Score Error Units
Log4JBenchmarking.logMessage thrpt 20 84.875 ± 6.383 ops/ms
Log4JBenchmarking.logMessage avgt 20 0.015 ± 0.001 ms/op
#2
Benchmark Mode Cnt Score Error Units
Log4JBenchmarking.logMessage thrpt 20 87.430 ± 9.362 ops/ms
Log4JBenchmarking.logMessage avgt 20 0.012 ± 0.001 ms/op
#3
Log4JBenchmarking.logMessage thrpt 20 79.753 ± 13.381 ops/ms
Log4JBenchmarking.logMessage avgt 20 0.013 ± 0.001 ms/op

—————————————————————–
Log4j2 Logger:
#1
Log4JBenchmarking.logMessage thrpt 20 75.881 ± 10.960 ops/ms
Log4JBenchmarking.logMessage avgt 20 0.012 ± 0.002 ms/op
#2
Log4JBenchmarking.logMessage thrpt 20 79.698 ± 12.290 ops/ms
Log4JBenchmarking.logMessage avgt 20 0.012 ± 0.002 ms/op
#3
Log4JBenchmarking.logMessage thrpt 20 87.428 ± 6.678 ops/ms
Log4JBenchmarking.logMessage avgt 20 0.012 ± 0.001 ms/op

—————————————————————–
Log4j Logger:
#1
Log4JBenchmarking.logMessage thrpt 20 72.490 ± 8.350 ops/ms
Log4JBenchmarking.logMessage avgt 20 0.014 ± 0.002 ms/op
#2
Log4JBenchmarking.logMessage thrpt 20 84.169 ± 9.227 ops/ms
Log4JBenchmarking.logMessage avgt 20 0.012 ± 0.001 ms/op
#3
Log4JBenchmarking.logMessage thrpt 20 72.599 ± 10.801 ops/ms
Log4JBenchmarking.logMessage avgt 20 0.012 ± 0.001 ms/op