Close

Spring - Logging in SLF4J + Logback

[Last Updated: Sep 10, 2017]

This example shows how to use Logback as an implementation provider for SLF4j in a Spring application.

Maven dependencies

 <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.8.0-alpha2</version>
        </dependency>
 </dependencies>

Checkout last tutorial to understand why all these dependencies are needed.

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");
  }
}

Logback configuration file

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

@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

17-09-10 20:40:23:977  INFO main o.s.c.a.AnnotationConfigApplicationContext:583 - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2c8d66b2: startup date [Sun Sep 10 20:40:23 CDT 2017]; root of context hierarchy
17-09-10 20:40:24:140 INFO 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.8.0-alpha2: JCL 1.2 implemented over SLF4J.
  • JDK 1.8
  • Maven 3.3.9

Spring Logback Logging Select All Download
  • spring-logback-logging
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • logback.xml

    See Also