Friday, June 10, 2011

SLF4J + Logback

I heard about LogBack some time ago and finally I had the chance to start using it. After some minutes of research, I was able to easily install and start to use this combination of tools, which looks like a much better logging system than the widely used Log4j. This post is a quick summary of that research.

Here you can read about the reasons to switch from Log4j to Logback. Basically LogBack is the natural evolution of Log4j, so just for that reason is enough for me to start being interested and investigate how to use this tool. All the benefits that comes from any evolution, will pay the bill of learning.

The Simple Logging Facade for Java or (SLF4J) is an abstraction for other logging frameworks, in this case I used LogBack, which will be the actual tool to perform the logging.

All what it is required in order to start using those combination of tools is to set up the dependencies. It means, you only need to add the jar files ( for example is used only slf4j-api-X.X.X.jar, logback-core-X.X.X.jar, logback-classic-X.X.X.jar where X.X.X is the current versions) in your project libraries or write the maven dependencies in your pom.xml as follows:

!-- Logging dependencies --
dependency
   groupId org.slf4j /groupId
   artifactId slf4j-api /artifactId
   version X.X.X /version
/dependency
dependency
   groupId ch.qos.logback /groupId
   artifactId logback-core /artifactId
   version X.X.X /version
/dependency
dependency
   groupId ch.qos.logback /groupId
   artifactId logback-classic /artifactId
   version X.X.X /version
/dependency

NOTE: I had to remove xml characters. :-(

Afte that all what you need is set up the configuration, but with LogBack there is a default configuration that applies if no configuration is found, this default configuration will print every log into the console (standard output). For example, basically similar to Log4j, all what it is necessary is a logback.xml in the classpath. Check here for more details about configuration. Then everything is ready to start creating the Logger instances (Using SLF4j api) in any class where you need to write logging. For example: 

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 
public class HelloWorld { 
    public static void main(String[] args) { 
           Logger logger = LoggerFactory.getLogger(HelloWorld.class);
           logger.info("Hello World"); 
    }
}

Note this code uses the SLF4j API, as it is the abstraction and automatically will check which logging framework library is in the class path in order to perform the actual job through that framework. 

Just as a hint. Do a small design about how to use the logging and for what. A good reference about how to plan this design is in the book of Growing Object-Oriented Software Guided by Tests page 233. Where explains de difference between Supoort logging (errors and info) and Diagnostic logging (debug and trace). Here also recomends to think about using notification rather than Logging. 

This post is just a quick reference to SLF4 + LogBack. However I really recommend the book even when not using Test Driven Development, because the book come with lots of good recommendation about good practices of software development in a more genearl point of view. 

Posted by Marc Andreu.

No comments: