Monday, April 24, 2006

Did you ever wonder why we see this piece of code?

if(debug is enabled)
{

logDebug("xxxx.....");
}

I was wondering for a long time why it is being done?
I can control the log level at production using configuration files.
I can turn off debug level logging in the log4j or any decent loggers config files.
But still, the conditional check if(debug is enabled) is performed in any many places, which I thought absurd and redundant.

But the reason is even though the logger does not physically log; the logDebug("xxxx.....") call is actually made which checks whether to log Debug level or not. Though we also check the same what the API checks, the thing we save here is the parameter construction. If the parameters are more or built by concatenating strings, it can be costly at runtime for constructing these. Though the time to log is saved, the time for constructing these parameters is a cost to the system. Hence this check if(debug is enabled) is made to save some time.

No comments: