Logback setup

Logback has been created to be the log4j successor, and designed as a native implementation of the SLF4J API - at least in its "classic" flavor. Its strong relation to SLF4J makes it probably the most interesting logger framework to couple with SLF4J.

It is easy start working with Logback. Once you have set SLF4J up in your developing environment, you have tested it with its native simple logger and maybe also with JUL, the JDK standard logger, Logback will be just a piece of cake.

Here is a minimal application that logs a message and then terminates:
package test;

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

public class TestLogback {
    private static final Logger logger = 

    public static void main(String[] args) {
        logger.debug("Hello from Logback.");
I forgot to mention that slf4j-api-1.6.4.jar should be in the application classpath. If that was missing, you can't expect more than a spectacular failure:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at chapters.introduction.HelloWorld1.main(HelloWorld1.java:23)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Adding that jar to the classpath is not enough, though we have a slight improvement. SLF4J complains about no concrete framework available to do the real job:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Not knowing what to do, it defaults to the native no-logging plugin. Well, better than crashing.

We have to add to the application classpath another couple of JARs, Logback specific. To get them, we go to the Logback download page, download its compressed file (current latest version is 1.0.0) and expand it on your file system. The files we need to add to our classpath are logback-core-1.0.0.jar and logback-classic-1.0.0.jar.

The "classic" Logback JAR acts as a SLF4J plugin, working as an adapter from the SLF4J logging calls to the "core" Logback functionality. If no "core" is available, trying to run the application results in a crash:
Failed to instantiate SLF4J LoggerFactory
Reported exception:
java.lang.NoClassDefFoundError: ch/qos/logback/core/joran/spi/JoranException
at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:279)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:252)
The SLF4J LoggerFactory was trying to instantiate the Logback logger, but in the middle of its job it found out that there was something missing in it, and so it had to (brutally) reject the request.

No comments:

Post a Comment