Close

Spring - Using Logback with Slf4j API in pre Spring 5

Following example shows how to use SLF4j in spring core before Spring 5.

Spring 5 provided its own spring-jcl bridge which makes it easier to use other famous logging framework without a lot of efforts.

Before Spring 5 we had to exclude common logging dependency.

pom.xml

<project .....>
<modelVersion>4.0.0</modelVersion>

<groupId>com.logicbig.example</groupId>
<artifactId>spring-logback-logging-before-version-5</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10.RELEASE</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Note that, if we don't exclude commons-logging and don't add jcl-over-slf4j dependency then we can still use SLF4J API in our code, but Spring internal logging will still be going through JCL and that might end up in two different formatted output (JCL might switch to its default JUL logger).

A Bean using SLF4J API

package com.logicbig.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyBean {
  private static Logger log = LoggerFactory.getLogger(MyBean.class);

  public void doSomething() {
      log.info("doing something");
  }
}

src\main\resources\logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yy-MM-dd HH:mm:ss:SSS} %5p %t %c{2}:%L - %m%n</pattern>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="stdout"/>
    </root>
</configuration>

Main class

package com.logicbig.example;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ExampleMain {

  @Bean
  public MyBean myBean() {
      return new MyBean();
  }

  public static void main(String[] args) {
      ConfigurableApplicationContext context =
              new AnnotationConfigApplicationContext(ExampleMain.class);
      MyBean bean = context.getBean(MyBean.class);
      bean.doSomething();
  }
}

Output

25-11-11 22:34:42:240  INFO com.logicbig.example.ExampleMain.main() c.l.e.MyBean:10 - doing something

Example Project

Dependencies and Technologies Used:

  • spring-core 4.3.10.RELEASE (Spring Core)
  • spring-context 4.3.10.RELEASE (Spring Context)
  • logback-classic 1.2.3 (logback-classic module)
  • jcl-over-slf4j 1.7.6 (JCL 1.1.1 implemented over SLF4J)
  • JDK 1.8
  • Maven 3.9.11

spring-logback-logging-before-version-5 Select All Download
  • spring-logback-logging-before-version-5
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
    • pom.xml

    See Also