When to use log.isDebugEnabled?

One of the first coding-related question my partner asked me when I joined my current project: do you use log.isDebugEnabled()? This is a recurring question, and my answer is generally the same: “No.” Still, there is some nuance in this no. I will try to get to the point today and forget my usual digressions.

Useless…

There is hardly anything heavier on the eyes than the following:

Why is that? Because log.debug("Anything") already performs the test you just added. But then, what is the purpose of this function?

… or not

Easy case:

There! Building the message to log came at a high cost, so you just avoided building it in case it will not be needed.

Be sure to remember the order statements are evaluated

Now, if you are a smartass and try to beat the system by writing a method:

You may think you do not need the test here, since the test is made in the .debug(Object) method, but be sure to remember that any parameter is evaluated before the method is called.

This means the concatenation will be performed before the .debug(Object) method is called. So, be sure to use the test in cases like that.

Published by

Cyrille Chopelet

Programming addict, UX philosopher, casual gamer, sci-fi enthusiast, hi-tech dilettante, ... Some people even call me a geek.

2 thoughts on “When to use log.isDebugEnabled?”

  1. Ahhh, but there is but yet another performance buster!! Yes, inside the debug() method, a check is performed for you, BUT if you do
    log.debug(“My big time db object is ” + bigTime);
    or
    log.debug(“The results are ” + convertSomething(bigTimer) );

    without checking first if debug is on, all arguments passed into the debug method are evaluated first. Normally not a big deal, but I’ve seen times where it ends up biting you in the buttocks when you receive a call at 3AM on a Sunday morning because the server crashed or a big customer is dead in the water.

    I’ve found it a great practice to check if it is on first, all the time.

"Wit beyond measure is man's greatest treasure." − Rowena Ravenclaw