12

Possible Duplicate:
In log4j, does checking isDebugEnabled before logging improve performance?

I have seen people using log4j in the manner below:

if(logger.isDebugEnabled())
{
    logger.debug(" message ");
}

However, I checked the documentation for the logger.debug API and found that it checks if debug is enabled before logging the message. In that case, what is the point of writing extra the if?

Wouldn't it be exactly the same to just write

logger.debug(" message ");

?

Community
  • 1
  • 1
rbhawsar
  • 805
  • 7
  • 25
  • 2
    Have you looked it in google? It took me 10 seconds to find the blog post including the answer. http://livingtao.blogspot.com/2007/05/when-to-call-isdebugenabled-before.html – BartoszMiller Nov 07 '12 at 10:52
  • 2
    Shouldn't it be `logger.isDebugEnabled()`? – halex Nov 07 '12 at 10:54

6 Answers6

21

If you're really just writing

logger.debug(" message ");

then there's no benefit. But consider:

logger.debug("This: " + message + " happened on thread " + thread +
            "because " + cause);

You don't want that string concatenation (and more expensive conversions, potentially - think about things that you might want to log, but which are costly to convert to strings) to be executed if the result is just going to be thrown away.

If logging has a significant performance impact in the normal case, that's a disincentive to add logging code which could really help you in situations where suddenly you want to turn diagnostics on.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    A good reason to switch to logback and do like this: `logger.debug("A good reason to {} to {}", "switch","logback");`. More about it here: http://logback.qos.ch/manual/architecture.html#parametrized. – Mister Smith Nov 07 '12 at 12:31
7

I think the idea here is to prevent any parameter evaluation - in your case it's simple, but for example let's say it was more like:

logger.debug("Foo" + bar() + " bar: " + foo());

Okay this is contrived, but if debug is not enabled, you may not want to execute bar() and foo() and the string concatenation...

Nim
  • 33,299
  • 2
  • 62
  • 101
  • 2
    In fact log4j documentation tells that the purpose is just that. Not that method calls are needed, just the string concatenation is enough of an overhead that checking the boolean before doing it is way better. Also, the overhead is most likely to be wasted in test environments but to save time in production, so it is an actual improvement in production. – SJuan76 Nov 07 '12 at 10:52
2

One reason might be because there is some overhead in building the message. For example:

if(debug.isDebugEnabled()) {
    String message = buildLogMessage()
    logger.debug(message);
}

If buildLogMessage() takes a while to run you might not want to run it when you know the output is never going to be logged.

However, a better way to solve this is using an ObjectRender so that the conversion to String is only done when the value is going to be logged.

David Webb
  • 190,537
  • 57
  • 313
  • 299
  • I think you have a typo in the code. Should it not be `if(logger.isDebugEnabled()) {` instead of `if(debug.isDebugEnabled()) {` – Deer Womb Nov 19 '20 at 20:21
1

If you take a little time to see the implementation of log4j, you can know what the reason of that. When you write: logger.debug(" message "); The log4j appenders always append loggingEvent. It's expensive conversions. A if condition will be best, but useful in some cases.

// AppenderAttachableImpl.class
public int appendLoopOnAppenders(LoggingEvent event) {
        int size = 0;
        if (appenderList != null) {
            size = appenderList.size();
            for (int i = 0; i < size; i++) {
                Appender appender = (Appender) appenderList.elementAt(i);
                appender.doAppend(event);
            }

        }
        return size;
    }
Vu.N
  • 149
  • 4
1

Another reason is to modify the behaviour of the HotSpot VM. Not quite sure how it works but if you are running in production mode (debug logging off) - the HotSpot VM should optimise the code as if the if statement and everything inside was not there.

0

Before the debug method is called, it could be executed code that could be avoided with a conditional sentence, for instance, the string concatenation. Example:

log.debug("Value of variable1:"+variable+" Value of variable2:"+variable2);

If you add the condition if log.isDebugEnabled(), the string concatenation is not executed.

That is the only benefit.

David García González
  • 5,164
  • 8
  • 29
  • 35