0

Out of curiosity, is there any benefit when logging (using log4j) to use the above approach:

if (LOGGER.isDebugEnabled()) {
     LOGGER.debug("New Session Created");
}

rather than just:

 LOGGER.debug("New Session Created");

Is it merely a (potential) performance enhancement and would it still be relevant? Should this be used to wrap ALL debug output?

Thanks for your thoughts!

StuPointerException
  • 7,117
  • 5
  • 29
  • 54
  • 1
    possible duplicate of [Is Log4j isDebugEnabled() necessary before using logger.debug()?](http://stackoverflow.com/questions/17850614/is-log4j-isdebugenabled-necessary-before-using-logger-debug) – devnull Mar 24 '14 at 10:43
  • Also see http://stackoverflow.com/questions/963492/in-log4j-does-checking-isdebugenabled-before-logging-improve-performance – devnull Mar 24 '14 at 10:44

1 Answers1

1

In your use case, no, there's no benefit whatsoever.

However, if you are constructing a new string, for example, "New Session Create" + SessionID, then you'd want to avoid the string construction and rather use the boolean check.

It's a small difference, but in a large application, this string construction, and the associated garbage collection, could become a point of performance impact. If you pass static strings, however, you are actually creating a duplicate check, which, while also small, has possible performance impact.

Ewald
  • 5,691
  • 2
  • 27
  • 31