Monday, August 12, 2013

Spring based applications and logging dependencies

There is not much to say on the topic of Spring based applications and adding logging dependencies apart from pointing to the excellent blog entry at the Spring site itself - http://blog.springsource.org/2009/12/04/logging-dependencies-in-spring/

This is an old entry, but is still valid. The essence of the article is that Spring has one core logging dependency - on Apache Commons Logging. Given this dependency, there are two good options for Spring based applications.

1. Use slf4j with any of the slf4j supporting logging frameworks. This can be done by using the jcl to slf4j bridge library that slf4j provides, along with any of the compile time binding to a logging framework. say for eg, with log4j the following would be the maven dependencies:

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>${spring.version}</version>
 <exclusions>
  <exclusion>
   <artifactId>commons-logging</artifactId>
   <groupId>commons-logging</groupId>
  </exclusion>
 </exclusions>
</dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>jcl-over-slf4j</artifactId>
 <version>1.7.5</version>
</dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-api</artifactId>
 <version>1.7.5</version>
</dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-log4j12</artifactId>
 <version>1.7.5</version>
</dependency>
<dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.17</version>
</dependency>

There are 5 dependencies here - the commons-logging is being explicitly excluded first, then a jcl-over-slf4j library provides a JCL adapter to the slf4j library, then the slf4j api is required, the binding to log4j and finally the log4j library.


2. A better option is to use logback as the logging library. Logback natively supports slf4j api, so the dependencies is much more simplified:

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>${spring.version}</version>
 <exclusions>
  <exclusion>
   <artifactId>commons-logging</artifactId>
   <groupId>commons-logging</groupId>
  </exclusion>
 </exclusions>
</dependency>
<dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>jcl-over-slf4j</artifactId>
 <version>1.7.5</version>
</dependency>
<dependency>
 <groupId>ch.qos.logback</groupId>
 <artifactId>logback-classic</artifactId>
 <version>1.0.13</version>
</dependency>

References:
Springsource Blog entry by Dave Syer: http://blog.springsource.org/2009/12/04/logging-dependencies-in-spring/

Slf4J legacy bridge: http://www.slf4j.org/legacy.html

Logback logging library: http://logback.qos.ch/

No comments:

Post a Comment