Tuesday, December 11, 2012

Efficient Logging in Java

Logging is very important part of any programming language. It gives important information to developers to understand the program behavior. Its almost next to impossible to debug issues without logs. So logs are very important but logging itself is overhead for any program. It shares valuable CPU time, consume program memory, perform I/O operations. So Logs are necessary evils. 

Various logging framework provide various techniques to reduce these overhead. Common techniques to improve performance

  • Configuration Techniques
    • Set default logging level to Severe
    • On demand enable Logging for specific module
  • Programming Techniques
    • Use discretion while putting log level for evey log
    • Check log level before processing logging command
Recently I come across some other ways to improve logging efficiency. I tried to run few tests around them. Here is test case result:

Test Code using "java.util.logging.Logger"
Test Results "java.util.logging.Logger"
Test Results "java.util.logging.Logger"
Test Code using "org.apache.logging.log4j.Logger"
Test Results "org.apache.logging.log4j.Logger"

Conclusion
  1. Apache Logger is more efficient in logging than java default logger.
  2. String concatenation technique in less efficient than new placeholder technique. [But for log4j placeholder technique is available in  log4j 2.0 only.]
  3. Its very efficient to check log level before logging command for dynamic logs [The logs which require string concatenation or placing values in placeholders ]
  4. Its inefficient to check log level before logging command for simple/static logs.
Reference:
http://logging.apache.org/log4j/2.x/performance.html

No comments:

Post a Comment