isDebugEnabled usage

Dmitri Dolguikh dmitri at redhat.com
Thu Mar 25 16:01:10 UTC 2010


On 03/25/2010 12:38 PM, Jesus M. Rodriguez wrote:
> I saw some code yesterday that reminded me of the usage for isDebugEnabled().
>
> The code is:
>
>      private void debugMessage(String msg) {
>          if (log.isDebugEnabled()) {
>              log.debug(msg);
>          }
>      }
>
> Then a bunch of calls to that:  debugMessage("some message here");
>
> This sort of defeats the purpose of wrapping the log.debug(). While I've always
> been a fan of ALWAYS wrapping log.debug(), I've gone with the wrap them
> if the strings are being concatenated.
>
> See the discussion on spacewalk list:
> http://www.mail-archive.com/spacewalk-devel@redhat.com/msg01204.html
>
> So for candlepin, let's go with just doing log.debug("simple string")
> when needed
> and wrap the ones where you are dealing with concatenation i.e.
>     log.debug("Found token in curator " + subToken + "  " + token);
>
> Some would argue this is premature optimization, but having had to go through
> this with 3 projects now, I'd like to go ahead and just head off this now before
> we have 100k of code.
>
> Thanks
> jesus
>
> PS sloccount shows
>
> Totals grouped by language (dominant language first):
> java:         10370 (90.61%)
> python:         711 (6.21%)
> sh:             249 (2.18%)
> ruby:           115 (1.00%)
> _______________________________________________
> candlepin mailing list
> candlepin at lists.fedorahosted.org
> https://fedorahosted.org/mailman/listinfo/candlepin
>    
from log4j documentation 
(http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Category.html#isDebugEnabled%28%29):

public boolean*isDebugEnabled*()

Check whether this category is enabled for the |DEBUG| Level.

This function is intended to lessen the computational cost of disabled 
log debug statements.

For some |cat| Category object, when you write,

       cat.debug("This is entry number: " + i );
   

You incur the cost constructing the message, concatenatiion in this 
case, regardless of whether the message is logged or not.

If you are worried about speed, then you should write

          if(cat.isDebugEnabled()) {
            cat.debug("This is entry number: " + i );
          }
   

This way you will not incur the cost of parameter construction if 
debugging is disabled for |cat|. On the other hand, if the |cat| is 
debug enabled, you will incur the cost of evaluating whether the 
category is debug enabled twice. Once in |isDebugEnabled| and once in 
the |debug|. This is an insignificant overhead since evaluating a 
category takes about 1%% of the time it takes to actually log.


so you are correct on both points!
-d

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.fedorahosted.org/pipermail/candlepin/attachments/20100325/b4b71cb1/attachment.html>


More information about the candlepin mailing list