Skip to end of metadata
Go to start of metadata
This page provides a quick guide for setting up SLF4J and LOGBack in a Maven web project in five simple steps.

Logback is intended as a successor to the popular log4j project. It was designed, in addition to many individual contributors, by Ceki Gülcü, the founder of log4j. It builds upon experience gained in building industrial-strength logging systems going back as far as 1999. Logback-classic natively implements the SLF4J API so that you can readily switch back and forth between logback and other logging frameworks such as log4j or java.util.logging (JUL).

The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, e.g. java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time.

If your working with a Maven web-app project, this procedure will get you setup to log with LOGBack through SLF4J super fast.

Step 0 - Add LOGBack dependency libraries.

If you are using Maven skip this step.

Import the following libraries to your WEB-INF/lib folder:

    • lib
      • logback-classic.x.x.x.jar
      • logback-core.x.x.x.jar
      • slf4j-api-x.x.x.jar

Step 1 - Add LOGBack dependency to your Maven POM

Declare the following dependency in your Maven 2 pom.xml and Maven will grab the appropriate libraries for you during the build.


Step 2 - Import existing (starter) XML configuration files

You will likely want to start with a base configuration file that you can build upon. In Maven you can have a logging configuration for your main source and another for your testing. You can download starter configuration files for your project by clicking the links in the hierarchy below. Put them in your project according to the position indicated by the hierarchy shown.

Step 3 - Customize the XML configuration just enough to test

Open up the logback.xml file. If you used the starter provided in the link above, you'll find the following:

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

  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
  <logger name="com.base22" level="TRACE"/>

  <root level="debug">
    <appender-ref ref="STDOUT" />

You will notice that one logger is defined at a package level ("com.base22"). You can simply change that to match your application's package base. You can also declare additional loggers (packages and/or classes) if desired.

Step 4 - Put logging code in your classes

The last thing you need to do is drop some logging code in a class and test this whole setup.

Add the following to the imports section of your java code:

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

Add the following at the top of your class in the global section (just under the line that declares your class public class Whatever extends Whatever). Change the name of the class (MyClassName) in the getLogger method call, of course. Name it the same as the class you're dropping this code into.

static final Logger LOG = LoggerFactory.getLogger(MyClassName.class);

Throw some logging statements in your code somewhere where you know they'll be fired right away when you run your app. For example:

LOG.trace("Hello World!");
LOG.debug("How are you today?");"I am fine.");
LOG.warn("I love programming.");
LOG.error("I am programming.");

Alternatively, you can just download this simple console test app and run it as a Java app from the command line or from within your IDE:

This class has a main method so it runs as a Java app and it will log one statement at each level.

Step 5 - Run your app and make sure it works

Finally, run your app and make sure it works. You should see log lines in your console. If it doesn't work, just review these steps a little more carefully and fiddle with it.

Enterprise Solution Architecture
Cody Burleson is an Enterprise Web Architect, entrepreneur, and writer. He designs and builds solutions with his team at Base22 using IBM WebSphere Portal, IBM Web Content Manager, IBM Connections, and Java/J2EE. He is a tireless student of information technology who loves to invent things, improve things, and share what he learns. You can find more at his blog,

Related Content

The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks.

Java Logging Standards and Guidelines
Following these guidelines can help you make your logging output as consistent as possible across classes.

How to setup Log4j in a web app - fast
This page provides a quick guide for setting up Log4j in a web application. It is intended for those who are already familiar with Log4j and just need a rapid reminder.

Logging with SLF4J
A very good overview blog post from @Blog("Baptiste Wicht").

SLF4J: Now you can do logging in Java... oh wait!
Great thread from the ServerSide with a wealth of opinions about various logging frameworks.


  1. Unknown User (orlando.ramirez)

    If you want to save the log into a file you need to add the following:

     <appender name="STDOUT" class="ch.qos.logback.core.FileAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
          <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>

    You must alse add an appender to the root level:

    <root level="debug">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
  2. Anonymous

    Little correction (name "FILE" instead of "STDOUT" for this specific appender):

    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
      <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
  3. Anonymous

    Great post, it helped put everything together. One question though, where does the log file get written to.

    1. Put a full path instead of just a file name in the <file> node shown in Orlando's comment.

      1. Anonymous

        Ok, thanks for that, do you know what the default is though?

  4. Anonymous

    Thanks for the instructions, however when I followed them, I got NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder. I added logback-core in the pom, but it didn't help

    1. You need all 3 jars in your lib directory: logback-classic, logback-core, and slf4j-api. I just did this with a brand new project without any issues at all.

  5. Anonymous

    I just have a question, can i generate an Id for each line in my log file with the PatternLayout or any logback's node ?
    Last question, if i want to replace %d(HH:mm:ss) by the difference in milliseconds between the current time and My application's birthday, how can we do that.

  6. Anonymous

    i'm trying to use logback and slf4j with a webapp (witch is not maven one) in glassfish. i have followd the steps 

    but i get the following error

    SCHWERWIEGEND: Class [ Lorg/slf4j/Logger; ] not found. Error while loading [ class ]
    WARNUNG: Error in annotation processing: java.lang.NoClassDefFoundError: Lorg/slf4j/Logger;

    in our web app. we have different structure than the one here explained
    our is with
    <------ should i but the logback.xml here
    or what should i do to get logfiles per-webApp

  7. Anonymous


    For your error :

    The logback.xml in a Web Application must be in :
    WEB-INF – > classes --> Here (logback.xml)
    you create a directory in WEB-INF rename it (classes) and put your configuration file on it. (smile)