{{ !articles[0].partner.isSponsoringArticle ? "Platinum" : "Portal" }} Partner

Adding SLF4J to your Maven Project

In yesterday’s blog, I documented the various bits of the Maven POM file created for a ‘Spring MVC Project’ using one of Spring’s project templates. In that POM file you may have noticed a reference to an slf4j-api artefact and comment to the effect that the Guy’s at Spring were excluding “Commons Logging in favor of SLF4j”. This blog takes a look at SLF4j, or to give it its full title: “Simple Logging Facade for Java” and demonstrates how to add it to a project.

SLF4j is not a logger in itself, it’s a facade or wrapper that delegates the actual business of logging to one of the more well known logger implementations, which is usually Log4J. The idea behind it is that’s its a replacement for that other well known logging facade: Commons Logging. The reason that it has been written as an alternative to Commons Logging, is that Commons Logging loads your logging library by using some whizzy Java ClassLoader techniques. Unfortunately, this has gained it a reputation for being somewhat buggy (although it has to be said I’ve never come across any problems with Commons Logging).

SLF4j, on the other hand, does things more simply; there’s no whizzy ClassLoader, you simply specify the slf4j API library, the one you use in your application, plus another SLF4j library that binds the first library to the logger implementation you’re using such as Log4J. The shortlist of binding libraries that SLF4j supports can be found here.

To add SLF4j to your project, the first thing to is to add in the SLF4j API


...which is pretty simple. The thing to note is that this must be in compile scope. The next step is to choose a binding library; one of the following:

<!-- Binding for NOP, silently discarding all logging. -->
<!-- Binding for System.out -->
<!--Binding for log4j version 1.2.x You also need to 
place log4j.jar on your class path. -->
<!--Binding for commons logging over slf4j -->

...and note that the scope here can be runtime.

If you add more than one binding JAR to your project config, then you'll get the following error message:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/Roger/.m2/repository/org/slf4j/slf4j-nop/1.6.1/slf4j-nop-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/Roger/.m2/repository/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.

...and alternatively, if you forget to add any bindings, the following message is written to Standard Err and all log messages are sent to nop (no/null output):

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

The following code demonstrates how to use SLF4j in an application, as you can see the fatal logging level has not been implemented.

public static void main(String[] args) {

    Logger logger = LoggerFactory.getLogger(Slf4jHelloWorld.class);

    logger.trace("Hello World");
    logger.debug("Hello World");
    logger.info("Hello World");
    logger.warn("Hello World");
    logger.error("Hello World");

SLF4j is well documented and you can find all the manuals and what-not on their site.

Finally, at the time of writing, the lastest version of SLF4j is 1.6.2; however, this only seems to be available from the SLF4j website. The lastest version available from Maven Central is 1.6.1.


From http://www.captaindebug.com/2011/09/adding-slf4j-to-your-maven-project.html

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks