2011年10月17日星期一

Log4j, isDebugEnabled check necessary when logging a debug message?

 

logger.debug() method in Log4j already checks isDebutEnabled, maybe it’s not necessary to check isDebugEnabled() before every debug() method.

But consider when there is very complex operations in the debug()’s message construction. When calling the debug() method, the construction logic of the message are always executed first. It’ time and space consuming.

So, practically speaking, in situations when the debug message’s construction logic is not complex, for example, a simple constant string, the isDebugEnabled() check before the logging is not necessary.  On the other hand, in situations when the debug message’s construction logic is complex, for example, involving string buffer and loops, the isDebugEnabled() check before the logging is definitely necessary.

In practice, I would prefer to make a consistency to alway add the check before the real message logging.