Skip to main content

Using SLF4J for Android

Posted by fabriziogiudici on May 12, 2011 at 3:40 AM PDT

Two years ago I was still puzzling about which was the best logging facility for Swing. In particular, the problem was related to BetterBeansBinding and jrawio, two mainly Swing-oriented applications, thus related to the desktop. Being these projects two small libraries, they shouldn't impose any further dependence on the application. The choice at the time was to stick with java.util.logging because it is already in the runtime and in the desktop world I hadn't frequently seen other logging facilities. A thin home-made wrapper provided better performance by using templates for message formatting, even though I felt I was re-writing yet another logging wrapper... The choice for those two projects is probably still appropriate, while I felt unsatisfied for all my other stuff.

Indeed, I've been using java.util.logging for several years, also influenced by the fact that Jini was using it in 2004, when I worked on the Jini RTTS project in Formula One. At the time I had just started my cluster of open source projects, so I preferred to stick with a single choice. It's well known that java.util.logging is probably the worst of the existing Java logging facilities and I've been thinking about a replacement for some time. Then I received the final push a month ago, when my friend Paolo Predonzani gave a very effective talk at JUG Genova about all the major logging facilities in Java.

So I've now made a decision for a new standard for my projects: SLF4J for the API and LogBack as the back end. I've started to replace the code in existing projects (which will require some time: SLF4J offers some automated code replacement tool for migrating from one of the major logging libraries, but of course not for my home-made wrapper).

An immediate bonus is that SLF4J has got a port for Android which I've started to use for blueBill Mobile. It just plugs into the native Android logger facility and seems to work well. Properly configuring Maven POMs in the project, the code is now using SLF4J, bound to the native logger when running on the device, and to LogBack when running unit and integration tests independent of Android. It looks like good stuff.

PS After a pause of several months, I've actively resumed work on blueBill and 1.0.BETA should see the light soon.

Related Topics >>