Blog4Java

A personal and Java blog, likely only for me

Spring logging with SLF4J and Logback

| 0 comments

As you already know, Spring framework uses Commons Logging (JCL, the J stands for Jakarta, the former house for Apache Java solutions) as the framework for logging, mainly for historical reasons and backward compatibility.

But it’s possible to use another framework easily thanks to the existing binding process for most of the popular frameworks.

Log4J

If you want to use the classic and popular framework it’s very easy, since Log4J can be used directly with JCL (I’d rather commons-logging, BTW)

Just add the dependency, no need of excluding anything from Spring. For instance, in a maven project:

Don’t forget to put a configuration file ( log4j.properties or log4j.xml) in the root of the classpath.

SLF4J with Logback

These two frameworks have became my favourite ones for Java logging.

However, in order to use them, you have to make a little bit more changes.

  1. Exclude CL from Spring
  2. Bridging legacy logging APIs, that is, redirect log4j and java.util.logging calls to SLF4J.
  3. Include the SLF4J API dependency
  4. Include the Logback dependency

Since Logback implements SLF4J natively, there is no need of further binding.

Finally, you can configure logback with a logback.groovy in the classpath, a logback-test.xml in the classpath, a logback.xml in the classpath or using the BasicConfigurator.

Spring provides instructions to use SLF4J with Log4J and there is a great explanation for the bindings at SLF4J logging with Log4J and JCL

Author: Javier (@jbbarquero)

Java EE developer, swim addict, occasional videogames player, fan of Ben Alex and Shamus Young, and deeply in love with my wife. Sooner or later I'll dedicate a post to expand this simple introduction.

Leave a Reply

Required fields are marked *.