Spring - Logging in SLF4J + Logback

[Updated: Sep 10, 2017, Created: Sep 10, 2017]

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

Maven 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() {"doing something");

Logback configuration file


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

Main class

public class ExampleMain {

  public MyBean myBean() {
      return new MyBean();

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


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

See Also