30

When I was going through some code, I noticed the use of logger as follows,

if(logger.isDebugEnabled())   
    logger.debug("Something..");

But in some codes, I observed like this.

logger.debug("Something..");

When I looked at the source of log4j, in the debug() method of Logger itself if(logger.isDebugEnabled()) was checked. Then why do we need this unnecessary overhead if(logger.isDebugEnabled())??

prasanth
  • 3,502
  • 4
  • 28
  • 42

5 Answers5

56

It's useful when the String your passing to logger.debug(...) takes time to evaluate, in that case you can skip this evaluation if debug is not enabled.

if(logger.isDebugEnabled()) {
    logger.debug("The meaning of life is " + calculateMeaningOfLife());
}

IMO this makes the code a lot less readable so it should only be used when there's a significant performance improvement.

Edit 10 years after the original answer:

Now that we have lambdas, loggers have added support for suppliers that are only called when necessary. So the above code could be simplified to:

    logger.debug("The meaning of life is {}", () -> calculateMeaningOfLife());

And the logger will only call calculateMeaningOfLife() if the message should be logged.

Guillaume
  • 14,306
  • 3
  • 43
  • 40
18

isDebugEnabled is typically used to avoid unnecessary String concatination, eg this call

logger.debug("Line number = " + n);

first invokes Strings concatination then debug() and only then Logger detects that debug is not enabled and simply returns. This may significantly affect app performance.

This problem is solved in SLF4J which has formatted logging methods like this

public void debug(String format, Object arg);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
6

Java must first resolve the string parameter passed to the debug method before it can call it.

logger.debug("Something.. var1=" + variable1 + "  var2=" + variable2);

The above code will result in multiple String objects being created as each + creates another String so you'll have about 5 or more objects created before calling the method.

Debugging will mostly not be enabled so it's more efficient to check if debugging is enabled than to resolve the parameters all the time.

Jean B
  • 326
  • 3
  • 4
4

The statement:

if(log.isDebugEnabled()){

Is used just for performance reasons. It's use is optional since it is called by the log method internally.

But now you ask if this check is made internally, so why should I use it? It's very simple: if you log something as simple as this:

log.debug("ResultSet rs is retrieved from OracleTypes");

Then you don't need to do any check. If you compose a string to log using the append operator (+) like this:

log.debug("[" + System.getTimeInMillis() + "] ResultSet rs is retrieved from OracleTypes");

In this case you should check if the log is enabled or not, because if it isn't, even if the log is not made, the string composition is. And I must remind you that the use of operator "+" to concatenate strings is very inefficient.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
1

SLF4J implementation (checked on version 1.7.10) calls isDebugEnabled() in some methods like:

public void debug(String format, Object... arguments) {
    if(this.log.isDebugEnabled()) {
        FormattingTuple ft = MessageFormatter.arrayFormat(format, arguments);
        this.log.debug(ft.getMessage(), ft.getThrowable());
    }
}

but there are also method overloads which doesn't internally check whether given loggin level is enabled, like in:

public void debug(String msg, Throwable t) {
    this.log.debug(msg, t);
}

Another thing is that Logger implementation can be changed so if you want to be always sure your logger is called according to it's logging level, then you might want to consider using isDebugEnabled() method.

Disper
  • 725
  • 12
  • 28