Tuesday, November 11, 2008

How to Use SLF4J with Jetty 6

In order to use Log4J with Jetty 6 (to get Jetty 6 and presumably subsequent versions of jetty to log output using Log4J), you use SLF4J which is integrated with Jetty and is a bridge (actually they call it a facade) to whatever particular logger you want to use. In order to use Log4J...

In {jetty.home}/lib put these jars (versions may vary):

slf4j-api-1.5.5.jar
slf4j-log4j12-1.5.5.jar
log4j-1.2.15.jar

In {jetty.home}/resources put your log4j.xml file

For instance if the place where you installed jetty is C:/jetty6 your director tree will look something like this:

C:/jetty6/bin...
C:/jetty6/lib/slf4j-api-1.5.5.jar
C:/jetty6/lib/slf4j-log4j12-1.5.5.jar
C:/jetty6/lib/log4j-1.2.15.jar
C:/jetty6/resources/log4j.xml
etc.

Your log4j.xml file may look something like this:

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

<?xml:namespace prefix = log4j /><log4j:configuration>
<appender class="org.apache.log4j.DailyRollingFileAppender" name="FILE">
<param name="file" value="logs/jetty.log">
<param name="datePattern" value="'.'yyyy-MM">
<param name="append" value="true">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n">
</layout>
</appender>

<root>
<priority value="debug">
<appender-ref ref="FILE">
</root>
</log4j:configuration>

You can download the above technologies at these web sites:

http://www.mortbay.org
http://www.slf4j.org/
http://logging.apache.org/log4j/1.2/index.html