Configure LogBack Logging with Spring

LogBack is an API for logging created by the same author of Log4j (a newer implementation, it is like a new version), during this article I’m going to show how to integrate it and use it on a Spring project.

On this tutorial I assume you are using a simple Spring ROO project which will prepare all the structure of the project for you, for more information see :

First of all you need to create the logback.xml (hold the configuration appenders like log4j.properprties) file in src/main/resources:


<?xml version="1.0" encoding="UTF-8"?>


    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">


            <pattern>%d %5p | %t | %-55logger{55} | %m %n</pattern>



    <logger name="test.myapp.repos">

        <level value="INFO" />


    <logger name="org.springframework">

        <level value="INFO" />



        <level value="INFO" />

        <appender-ref ref="CONSOLE" />



The second step is to configure Maven dependencies and add LogBack required libraries:

<-- Properties Settings -->









<-- Dependencies Settings -->































You need to get rid of all Log4j dependencies in the Maven pom.xml generated by Spring ROO, clean every single dependency related to logging before you add the code provided to set up LogBack.

For using the logger on a class you are developing, you need to create a static instance of it and use normally as you use the Log4J, the only difference is the implementation and configuration code of LogBack Vs Log4j. On Logback.xml your class must be scanned for the logger to work.

package test.myapp.repos; /*This package figures on LogBack.xml*/

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class MyTestClass {

    static Logger logger = LoggerFactory.getLogger(ItemController.class);


    public void create(String args) {

        logger.debug("My Args Is => " + args);



There is another sophisticated way of injecting the logger on a Spring bean, this can be achieved by developing a custom BeanPostProcessor which will automatically inject the Logger on fields annotated with @Log (this is a custom annotation we created) instead of instantiating the manually the logger as described earlier.
/** Custom @Logger annotation **/




public @interface Log { }

/** LoggerPostProcessor => Custom Spring BeanPostProcessor **/

public class LoggerPostProcessor implements BeanPostProcessor {

    public Object postProcessAfterInitialization(Object bean, String beanName) throws

        BeansException {

        return bean;


    public Object postProcessBeforeInitialization(final Object bean, String beanName)

          throws BeansException {

        ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {


                public void doWith(Field field) throws IllegalArgumentException,

                    IllegalAccessException {


                    //Check if the field is annoted with @Log

                    if (field.getAnnotation(Log.class) != null) {

                        Log logAnnotation = field.getAnnotation(Log.class);

                        Logger logger = LoggerFactory.getLogger(bean.getClass());

                        field.set(bean, logger);




        return bean;



/** Usage on a Spring Bean **/


public class MyBeanImpl implements MyBean {

    /** Not manual set up code **/

    @Log Logger myLogger;



The last thing to do is to declare this new BeanPostProcessor on you applicationContext.xml file :

<!-- The Logger Injector -->

<bean id="LogginInjector" class="ma.oncf.achat.utils.LoggerPostProcessor" />

For more information about why to switch to LogBack, see : Why switch to LogBack.

Reference: Configure LogBack Logging with Spring from our JCG partner Idriss Mrabti at the Fancy UI blog.

Related Articles :

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

One Response to "Configure LogBack Logging with Spring"

  1. jalalashrafi says:

    This could be used in spring managed beans, but a more generic solution would be using aspects.

Leave a Reply

− 1 = eight

Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books