« Subversion: Upgrade to 1.6 using 'svnsync', and move projects using 'svnadmin dump' | Main | Geek Tip of the Day »

April 24, 2011



Parameterized logging makes for "cleaner" code visually, but you still pay the price of potentially expensive object serialization. While the conditional wrapper does incur the cost of checking for debug logging twice, that boolean test is likely a LOT cheaper than performing the more expensive object serialization which just gets thrown away inside the method call.

If only Java had nicer closure syntax (i.e., not this anonymous inner class crap) and/or lazy binding and deferred evaluation ...

Michael Andrews

Absolutely. Checking a boolean twice is not very expensive overall. If you are passing a parameter that is expensive to evaluate (like object serialization) - then you still need to do the wrap. Thanks for pointing that out! :)

Javin @ garbage collection in Java

Excellent article , you have indeed covered topic in details with code examples and explanation. I have also blogged some of my logging experience as 10 tips on logging in Java



A few comments regarding your conclusions on Log4J:

1. Log4J ships with the AsyncAppender in-the-box. That more-or-less takes care of asynchronous logging.

2. Log4J is still being maintained, the most recent release (at the time of writing, May 2011) being 1.2.16, released in April 2010. However, the Log4J 2.0 project does seem to be moribund.

3. Extending Log4J is a piece of cake, e.g. you can provide your own Appender by simply subclassing the abstract AppenderSkeleton class. It's probably equally as easy as java.util.logging.

I'd be very interested to learn how you arrived at the conclusion that LogBack is ten times faster than Log4J? Is that only via the SL4J API? Presumably the difference is considerably smaller (i.e. less than an order of magnitude) when you use the native Log4J API?


Thank you, I like this post a lot.

Timberland For Women

Liked you on Facebook, too. =)


What happens with the tomcat webapp tips? ;)


Very nice article.
I was trying to understand the nitty gritty of the logging in java too. That said, I am having difficulty making SLF4j and logback work in JBOSS AS 7.1.2. ... as some of my dependencies are including other logging implementations.
I cannot seem to figure out how to exclude them.
If time permits, could you please give a tutorial on how to use it.


Michael Andrews

SGB - You'll need to bridge those other logging implementations to SLF4J. You'll then be able to pick the runtime implementation for your app. I use Logback at runtime. Here are my maven dependencies when I'm depending on JARs that use JUL, Commons, and Log4J. Hope this helps.

Michael Andrews

the system removed the POM tags - but you get the idea. Here is a link to the docs on this.



Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Your comment could not be posted. Error type:
Your comment has been saved. Comments are moderated and will not appear until approved by the author. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.


Post a comment

Comments are moderated, and will not appear until the author has approved them.

Your Information

(Name is required. Email address will not be displayed with the comment.)