Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are we checking if debugging or logerror is enabled?

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?

like image 567
Adam Avatar asked Dec 05 '25 10:12

Adam


1 Answers

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.

like image 197
Mureinik Avatar answered Dec 07 '25 00:12

Mureinik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!