Why you should NOT use log.isDebugEnabled()

Several people are telling you that you should wrap all logging statements by a isXXXEnabled method call (where XXX is the logging level) to prevent unecessary object creations. I do not recomment this!

-It’s not part of your business logic but a kind of optimization

-The code becomes less readable

-When you change the loglevel you also have to change the isXXXEnabled call (error prune)

-…and at least: This is a very boring job 🙂

Boring job!? Why don’t let some optimizer do this work for us!? And here comes http://sourceforge.net/projects/just4log/files/

This project comes with and AntTask adding the “missing” statements for us:

a();

logger.debug(“Got ” + input + “, will return ” + output);

b();

becomes

a();

if (logger.isDebugEnabled())

logger.debug(“Got ” + input + “, will return ” + output);

b();

Didn’t I say the isXXXEnabled statements are not needed? No! I didn’t! They are very very useful. I just said that they shouldn’t be written by the programmer itself.

Oh! The project is dead since 6 years, see forum (http://sourceforge.net/projects/just4log/forums/forum/288112/topic/1354439)

Because of that and that the project did not support SLF4J (Simple Logging Fascade for Java) I decided to relaunch the whole project: Tataaaaa, here it is: http://sourceforge.net/projects/just4log-ng/

You might say: “Hey! You don’t have to check the loglevel using SLF4J!”. Unfortunately this is not the truth:

Of couse you can use the parameterized messages SLF4J is supporting

logger.debug(“The new entry is {}.”, entry);

but if you’d like to log more than two arguments you have to use the vararg method which will create an Object[] holding your parameters (this is done automatically by the compiler). Another thing is autoboxing of primitive values:

int a=5, b=7, c=a+b;

logger.debug(“Sum of {} and {} is {}.”, sum, a, b);

What will happen here? The compiler will transform this under the hood into:

logger.debug(“Sum of {} and {} is {}.”, new Object[] { Integer.valueOf(sum), Integer.valueOf(a), Integer.valueOf(b) });

So there are 4 objects created you won’t see in your source code (3 Integers and one Object[])

Let me repeat myself:  The code should be wrapped by isXXXEnabled to prevent the Object[] creation.

I recommited the code found in the old repository and did a lot of optimizations (general rework, Java5 migration, command line support, fixed some bugs)

Advertisements
This entry was posted in Java, Projects, Tips. Bookmark the permalink.

2 Responses to Why you should NOT use log.isDebugEnabled()

  1. Thanks man…was looking for this. Will try this today..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s