Thursday, 22 September 2011

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

	<properties>
		<slf4jVersion>1.6.1</slf4jVersion>
	</properties>	
        <dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-api</artifactId>
		<version>${slf4jVersion}</version>
	</dependency>

...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:

	<!-- CHOOSE BETWEEN ONE OF THESE DIFFERENT BINDINGS -->
	<!-- Binding for NOP, silently discarding all logging. -->
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-nop</artifactId>
		<version>${slf4jVersion}</version>
	</dependency>
	<!-- Binding for System.out -->
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-simple</artifactId>
		<version>${slf4jVersion}</version>
	</dependency>		
	<!--Binding for log4j version 1.2.x You also need to 
	place log4j.jar on your class path. -->
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>slf4j-log4j12</artifactId>
		<version>${slf4jVersion}</version>
	</dependency>
	<dependency>
		<groupId>log4j</groupId>
		<artifactId>log4j</artifactId>
		<version>1.2.16</version>
		<scope>runtime</scope>
	</dependency>
	<!--Binding for commons logging over slf4j -->
	<dependency>
		<groupId>org.slf4j</groupId>
		<artifactId>jcl-over-slf4j</artifactId>
		<version>${slf4jVersion}</version>
		<scope>runtime</scope>
	</dependency>

...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.

7 comments:

Anonymous said...

Nice tutorial for beginner. However I would like to see logback as an example implementation instead of log4j. Log4j is no longer maintained, and logback is its successor. Also logbacks configuration is more sane and better documented than log4j:s.

Alexander said...

I'm not sure what you mean by slf4j is not maintained, the current version is from June 11, 2012

Roger Hughes said...

Alexander,
Not sure what you mean by saying "slf4j is not maintained" - I don't say that in my blog. The previous anonymous comment said that "Log4j is no longer maintained" and that they'd "see logback as an example implementation".

Alexander said...

sorry, i confused log4j and slf4 from the first comment
log4j is no longer maintained, of course

Alexander said...

I take that back, Log4j 1.x has a released a new version this May.

Anonymous said...

I get the following error message.

log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Can you please help?

Roger Hughes said...

Anonymous
Have you configured your underlying log4j properties file and is it on the class path?