I always see it is better to check whether log.isDebugEnabled
before adding a log.debug
statement.
I guess it should be taken care by the logging framework, could you please let me know what is the advantage of having this condition check?
I always see it is better to check whether log.isDebugEnabled
before adding a log.debug
statement.
I guess it should be taken care by the logging framework, could you please let me know what is the advantage of having this condition check?
Consider the following example:
if (log.isDebugEnabled()) {
log.debug("This is my complicated object" + veryComplicatedObject.toString());
}
String concatenation may be expensive (memory-wise), and there is no reason to perform it if we aren't going to log it anyway. Checking the logging level beforehand saves constructing redundant strings.
It optimization, doing a check simply saves cpu time in going to the class and checking to see if the level you want to report at is set or not. It only starts to make a different if you're using a lot of logging requests (tens of thousands or more) or multiple logging requests per section.