Maybe I am too picky about certain details. I investigated a project recently which has quite a few debug statements in there (which is good of course), but only about a quarter were preceded by “if ( log.isDebugEnabled() )”.

This is not ideal. No the absence of these checks will not make your application fail. No it will probably not kill performance. Using isDebugEnabled is a guideline and an indication of good coding.

The reason to use these is the overhead of parameter building. The “log.debug” statements actually start with a check whether debug logging is enabled, so they should finish very quickly when debug logging is off. Therefore you could say isDebugEnabled is not necessary. When just logging a static message this is true, in all other cases it is not. The often forgotten detail is that the parameters of the method call are calculated before doing the method invocation. This parameter construction could be slow (when calling other methods which happen to be time consuming), but more likely the main side-effect will not be performance loss but garbage creation. The string additions used will create garbage which need to be collected sometime and at that moment , your application may stall for a while, which is something to avoid if possible. Of course this depends n the garbage collection scheme used, but in all cases less garbage is better.

Some clever heads could counter this saying you should always use StringBuilder (or better still javolutions TextBuilder) to avoid the garbage creation. For logging statements this is complete overkill. It will just make your code less readable and it is a waste of typing effort for code which won’t be running in production anyway if you use the isDebugEnabled check. I mean, you do turn of debug logging in production don’t you?

Leave a Reply

Your email address will not be published. Required fields are marked *

question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen