Friday, April 8, 2011

Avoid premature processing in the logger statements

While logging the statements  we often dont notice the performance hit that logger statements can cause.

Sample code:
-- log debug
logger.debug("Testing  : " +someCalculations() );


The problem with the above statement is that even if the debug level is other than debug , the someCalculations() function will get called and its result is still being concatenated with the string “Testing: “, only to be discarded later.

It’s a general case of Java evaluating a method’s arguments first before executing the method itself.This is can be checked in both commons logging and log4j.

No comments: