Java Logging Wrapper with Static Binding – SLF4J

When think of logging wrapper for Java, I believe Apache Common Logging come in to most developers mind. Even if you don’t use it, the API/framework you use are very likely depend on it.

It able to auto discover your actual implementation, with the cost of complexity. The class loader and reflection based dynamic binding might behave unexpectedly in different application servers.

Why spend long time to understand a logging wrapper since it is never the main concern of your application?

Just switch to SLF4J – “Simple Logging Facade for Java”. As its name imply, it try to be simple instead of act smart with those class loading hack. It support for log4j, common-logging, Java util logging, Logback, etc.

If you code with SLF4J, your code will typically look like this:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Toolbox{
	Logger logger = LoggerFactory.getLogger(this.getClass().getName());

	public helloWorld(){"Hello World");

All you need to do is place the SLF4J API jar file (slf4j-api-1.5.2.jar), your logging implementation jar file (said log4j.jar), and SLF4J adapter for the logging platform of your choice (slf4j-log4j12-1.5.2.jar) into your classpath. And It is done.

If you would like to change to Java util logging, just delete the ‘slf4j-log4j12-1.5.2.jar’, and place the adapter for Java util logging (‘slf4j-jdk14-1.5.2.jar’) into your classpath. And it will work perfectly without recompile. The only things to watch out is: there should always be only one adapter in classpath.

Besides adapter for different logging API, SLF4J also come with a set of bridge for various logging API. Said you have use an API that depend Apache Commons Logging, your application is code with log4j. And you would like to use Java Util logging as actual logging implementation.

The steps are as simple as 123:
1) Delete the common-logging and log4j jar file from classpath, place the bridging jar file (jcl-over-slf4j-1.5.2.jar, log4j-over-slf4j-1.5.2.jar) into classpath
2) Place SJF4J API (slf4j-api-1.5.2.jar) into classpath
3) Place adapter for Java util logging (‘slf4j-jdk14-1.5.2.jar’) into classpath

All call to common-logging and log4j will be intercepted by SLF4J and pipe to Java util logging. And all is done with just static binding.

For those who curious on how it can be done, I will suggest you to open the bridging and adapter jar file to have a look. It is really a good real world example of mock classes and adapter pattern.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

2 Responses to “Java Logging Wrapper with Static Binding – SLF4J”

  1. Wow! Thank you!
    I always wanted to write in my blog something like that. Can I take part of your post to my blog?
    Of course, I will add backlink?

    Sincerely, Reader

  2. As long cite a reference to my blog, you are welcome to use the content from here.

Leave a Reply