Log5j is a modern facade over the most heavily used logging framework Log4j that not only provides a better interface for logging but also performs better. Even though Log5j has a lot of advantages, most of the projects (even at Amazon) uses Log4j. You will find logs like this all over the code base

log.info("this is the string with value " + value + " and it does stink with factor" + stinkfactor);

All those concatenations, yuck!

I found the interface of Log5j pretty awesome, specially when it allows to you log like

log.info("this is the string with value %s and it does not stink",value);

But out of curiosity, I wanted to know how good its performance is. I did a simple test and found some interesting results.

###Experiment Setup Unit tests with log4j configured with a FileAppender (similar to what every production environment uses). The log level was set to INFO (hence DEBUG was disabled). In each test, logs with different types of logging statements (with/without parameters) were emitted in a loop with a 2 million count.

Randomly generated numbers were used as parameters in each loop just to avoid some optimizations that could be done by compiler if you have constant strings.

Use case Performance
(test finished in msec)
Notes
Log 4j Log 5j
Debug log with parameters
no isDebugEnabled()
using String.format() to format log statement
6184 183 String.format() was used only for log4j to simulate what log5j does internally
Log5j does not need String.format()
Debug log with parameters
no isDebugEnabled()
not using String.format()
1350 265 For log4j, logging statement like "log text" + param1 + " and other param" + random() was used.
Debug log without parameters
no isDebugEnabled()
52 142
Debug log with parameters
with isDebugEnabled() check for log4j
128 183 isDebugEnabled() not required for log5j
Info log with parameters
using String.format()
50192 45667 String.format() was used for log4j to simulate what log5j does internally
Log5j does not need String.format()
Info log with parameters
not using String.format()
34202 52207 -
Info log without parameters 31684 30958

###Conclusion

  • Do not use String.format() with log4j, specially when that log level can be off in production (E.g. DEBUG is off in production). Its very expensive.
  • Log5j performs almost the same as Log4j even without any isLOGLEVELEnabled() (E.g. isDebugEnabled()) checks. Hence, it results in clean code without redundant log enabled checks.
  • Log5j provides a cleaner interface to log statements than Log4j (with a mess of appending variables within log text) with a small bearable overhead