Evolution of Logging in Java
April 6, 2011 1 Comment

Slalom Consultant Brett Hovenkotter has extensive experience as a Development Team Lead across a broad range of methodologies, technologies and architectures.
When I first started doing development in Java back in 2000, there wasn’t a free logging framework available (at least not that I was aware of) so my development team wrote our own. Building a robust logging framework is not a trivial task, and thankfully Ceki Gülcü stepped up with log4j which was hosted by Apache and reached 1.0 in January 2001.
Java eventually got an official logging API in version 1.4 a year later, but as is common with features that make their way into the SDK, it was too late and less robust than what the open source community had already come up with.
The diversity in logging implementations was a headache for the growing population of libraries and frameworks, most of which wanted to contribute to log messages. The solution was the Apache Commons Logging API that provided a simple Façade that would detect which logger was being used and direct log messages to it.
For years I used these tools for logging without ever considering looking elsewhere, but recently a colleague introduced me to SLF4J, which like Commons Logging provides an API to be used over the top of various implementations. The big difference is that it solves one of logging’s biggest annoyances: DEBUG level log messages.
For example, in order to optimize performance I try to surround all of my DEBUG level log messages that include string concatenation with an if statement like this:
if (logger.isDebugEnabled) {
logger.debug(“Found foo with ID: “ + id);
}
SLF4J employs printf-style formatting introduced in Java 5, so now I can safely save two lines of code for each DEBUG message:
Logger.debug(“Found foo with ID: {}”, id);
This may seem trivial, but developers can be a lazy lot and eliminating boilerplate lines of codes makes them more likely to write useful log statements.
And while SLF4J replaces Commons Logging, Logback is a new logging implementation meant to replace log4j (in fact it was also developed by Ceki Gülcü as its natural successor) and has a number of performance and feature enhancements over its ancestor.
Overall the lesson here is never to grow too comfortable with a certain technology, especially as it grows long in the tooth. Every time you start a new project spend a little time looking at the “new hotness” to determine if there’s something new out there that better suits your needs.
Subscribe to be emailed about new Software Development posts.




There is a lot of logging frameworks but it’s hard to find good and free log viewer. You can try to use Apache Chainsaw but it’s old and unmaintained. Better solution is OtrosLogViewer (http://code.google.com/p/otroslogviewer/). It’s best feature is reading logs from remote severs using SSH or Samba and log message formatting (see video: http://www.youtube.com/watch?v=ESCa-X8l4OE&hd=1).