slf4j Notes

For my notes on log4j, see log4j.html. That project was successfully completed. However, I found that log4j suffers from severe performance penalties when things like classname, method name, line number, etc. are to be included in the log. That is why I've started to investigate slf4j.


Using slf4j

slf4j logging is pretty easy and promises to be a lot more performant than log4j. Here's a discussion: http://xhab.blogspot.com/2007/03/new-logging-experience.html. It's a very thorough treatment (overview and discussion).

I'm presently investigating. The documentation seems very clean, however, it is severely tainted by log4j baggage: the author assumes that you're coming to slf4j from log4j or another system and not that you're ready to start fresh. In fact, at this point, I don't even know if you can isolate your usage to just slf4j, but my preliminary attempts to write sample code for it appear to support that you can. I can't seem to get debug messages to come out, only info for now. See sample code and console output below.

Other crucial links to slf4j:

Source code sample

Here is the sample code for the output we'll be studying.

	package com.etretatlogiciels.samples.slf4j;

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


	public class SLF4J
	{
	  Integer             cur, old;
	  final static Logger logger = LoggerFactory.getLogger( SLF4J.class );

	  public void setTemperature( Integer temperature )
	  {
	    old = cur;
	    cur = temperature;
	    logger.debug( "Temperature set to {}. Old temperature was {}.", cur, old );
	    if( temperature.intValue() > 50 )
	      logger.info( "Temperature has risen above 50 degrees." );
	  }

	  public static void main( String[] args )
	  {
	    SLF4J  slf4j = new SLF4J();
	    slf4j.setTemperature( 60 );
	  }
	}

Console output

	29 [main] INFO com.etretatlogiciels.samples.slf4j.SLF4J - Temperature has risen above 50 degrees.