Thursday, September 27, 2007

JDK Logging Improvement

I've submitted an RFE to Sun for a simple addition to the JDK logging framework. If your code is logging intensive then you should love this addition. Don't you simply hate wrapping a conditional statement around your logging code to avoid unnecessary string concatenation like this:
if(logger.isDebugEnabled() {
   logger.log(Level.DEBUG, "Hello Brant " + string1 + string2 + object1 ");
}
If you hate doing this, then my RFE has a solution for you. My suggestion is to utilize printf-like syntax and varargs for a method signature of something like this:
logf(Level level, String format, Object... params)
Which would change the syntax to something like this:
logger.logf(Level.DEBUG, "Hello Brant %s %s %s", string1, string2, object1);
I would also include bonus points for being able to determine if the last object in the varargs list was a throwable and logging the stack trace. Or some other mechanism of including a throwable argument in the method signature. Maybe like this:
logger.logf(Level.DEBUG, "Hello Brant %s %s %s", string1, string2, object1, ex);
I'll admit that this is not entirely my idea. My fried Glen actually developed a logging wrapper framework which included this. He did a very nice job with it, I however am less inclined to utilize yet another log wrapping framework or force an internal logging framework onto users of my libraries. I'd prefer this just be available to people wanting to use standard JDK logging. I believe Glen has taken the idea to the SLF4J group. If you like this idea, I urge you to vote for the RFE at http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6594697