Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Use SLF4J logging with Spring

DZone's Guide to

Use SLF4J logging with Spring

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Long story short, Spring uses Commons logging as a logging API, but chances are that you are using SLF4J in your project, so here what you’ll need to do to make Spring use SLF4J instead of commons logging.

1) Remove all commons-logging dependencies
In maven, you’ll have to exclude the commons-logging dependency from each Spring module, e.g.

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>${spring.version}</version>
  <exclusions>
    <exclusion>
      <artifactId>commons-logging</artifactId>
      <groupId>commons-logging</groupId>
    </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>${spring.version}</version>
  <exclusions>
    <exclusion>
      <artifactId>commons-logging</artifactId>
      <groupId>commons-logging</groupId>
    </exclusion>
  </exclusions>
</dependency>

2) Add SLF4J and jcl-over-slf4j
jcl-over-slf4j Will do the binding between Spring and SLF4J.
In this example I use logback as the logging api.

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.1</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>1.7.1</version>
</dependency>

<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.0.7</version>
</dependency>


Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}