is isLoggable necessary here, and when I shoud use it?

I see lots of code like below, Does logging method itself do some cheap checking before logging the msg? I mean is it nessary to call isLoggable(...) everytime before the actural logging? Thx.

if (logger.isLoggable(Level.WARNING)) {

logger.warning("filename argument is required.");

}

[309 byte] By [javalatte1a] at [2007-11-27 7:51:12]
# 1
it's just to avoid the method call in case it is below the treshold anyway.this isn't really useful except in cases where the string you pass is e.g. the result of an expensive toString call
OnBringera at 2007-7-12 19:32:18 > top of Java-index,Java Essentials,Java Programming...
# 2
Or runtime concatenation of strings... Generally, it's good practice to do that for performances reasons. The check will prevent the code in the if statement from running if not needed.
bsampieria at 2007-7-12 19:32:18 > top of Java-index,Java Essentials,Java Programming...
# 3

It's only necessary to do that for performance reasons if logging takes up a significant proportion of your processing time. And if logging takes up a significant proportion of your processing time, you are probably doing way too much logging.

And you'll find that logger.warning starts out by calling logger.isLoggable anyway, so you're duplicating that effort. The only way that improves things is (as OnBringer already said) that you can avoid evaluating the parameters of the logger.warning() method. In your particular example you're doing an extra if-statement to avoid evaluating a string constant.

DrClapa at 2007-7-12 19:32:18 > top of Java-index,Java Essentials,Java Programming...
# 4

This is most useful for DEBUG level logging. If debug is not enabled you want to reduce its impact to a minimum (as debug logging tends to be very verbose).

Where I work we have a commit trigger that refuses to commit work that has unchecked debug logging (its useful).

In other cases it is useful if that section of code can produce significant qantities of log info (esp. logs that require string concat or expensive toString() calls). INFO levels should also have serious consideration for this as they can also be verbose and are normally disabled.

its generally not needed for WARN or ERROR logs as people tend to want those and they SHOULD be pretty rare.

matfud

matfuda at 2007-7-12 19:32:18 > top of Java-index,Java Essentials,Java Programming...