Jetty Logo
Contact the core Jetty developers at www.webtide.com

private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery

Example: Logging with Apache Log4j

This page describes how to Configure Jetty for Logging with Apache Log4j.

The basic configuration for Logging with Apache Log4j is to use Slf4j and the Slf4j binding layer for Log4j.

The Required JARs

There will be 3 JARs you need to download

slf4j-log4j12-1.6.6.jar

This is the Slf4j binding layer for Log4j.

slf4j-api-1.6.6.jar

This is the basic Slf4j API that Jetty's Slf4jLog implementation uses.

log4j-1.2.7.jar

This is the version of log4j that slf4j-log4j12-1.6.6 was written against.

Configuring Server Classpath

In order for the Server to use these new JARs you will need to put them into place and tell Jetty to load them on each startup.

Create a ${jetty.home}/lib/logging/ directory and place the 3 files you downloaded there.

[jetty-distribution-9.0.0.v20130308]$ ls -l lib/logging/
total 520
-rw-rw-r-- 1 jetty jetty 489884 Mar 11 15:40 log4j-1.2.17.jar
-rw-rw-r-- 1 jetty jetty  26176 Mar 11 15:40 slf4j-api-1.6.6.jar
-rw-rw-r-- 1 jetty jetty   9711 Mar 11 15:40 slf4j-log4j12-1.6.6.jar

Next, we need to edit the ${jetty.home}/start.ini to get jetty to load these logging files into the Server classpath.

Edit the start.ini and check for OPTIONS line with logging defined.

If you can't find a OPTIONS entry with logging, add one at the bottom of the file.

[jetty-distribution-9.0.0.v20130308]$ grep logging start.ini 
# Adding lib/logging to server classpath
OPTIONS=logging

Now verify that the 3 jars will be loaded by the server classpath.

[jetty-distribution-9.0.0.v20130308]$ java -jar start.jar --version | grep logging
Active Options: [Server, client, ext, jmx, jsp, logging, resources, websocket]
18:               1.2.17 | ${jetty.home}/lib/logging/log4j-1.2.17.jar
19:                1.6.6 | ${jetty.home}/lib/logging/slf4j-api-1.6.6.jar
20:                1.6.6 | ${jetty.home}/lib/logging/slf4j-log4j12-1.6.6.jar

Configuring Server Logging

If you have a ${jetty.home}/resources/jetty-logging.properties, make sure it contains just the following

# Configure Jetty for SLf4j Logging
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog

This will tell Jetty to use the Slf4jLog implementation for its internal Logging.

Configuring Log4j

Next you will need a ${jetty.home}/resources/log4j.properties file for configuring Log4j.

Example resources/log4j.properties file

# Basic Log4j Configuration with STDOUT and File logging
log4j.rootLogger=INFO, stdout, filer

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

log4j.appender.filer=org.apache.log4j.RollingFileAppender
log4j.appender.filer.layout=org.apache.log4j.PatternLayout
log4j.appender.filer.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.filer.File=${jetty.home}/logs/jetty.log
log4j.appender.filer.DatePattern='.'yyyy-MM-dd
log4j.appender.filer.MaxFileSize=10MB
log4j.appender.filer.MaxBackupIndex=4
log4j.appender.filer.append=true

Note

The use of ${jetty.home}/logs is a handy way to always get a reference to the standard Jetty logs directory

Scripted Installation of Log4j

#!/bin/bash
# -------------------------------------------
# FOR DEMONSTRATION PURPOSES ONLY
# USE AT YOUR OWN RISK
# -------------------------------------------

if [ ! -f start.ini ] ; then
    echo "ERROR: Not in \${jetty.home} directory"
    exit -1
fi

if [ -d lib/logging ] ; then
    echo "ERROR: Existing lib/logging directory exists."
    echo "       Remove any existing logging implementations"
    echo "       before running this script, as conflicting logging"
    echo "       implementations can cause problems."
    echo "       Then remove the lib/logging directory entirely."
    exit -1
fi

mkdir lib/logging
pushd lib/logging

curl -O http://repo1.maven.org/maven2/org/slf4j/slf4j-log4j12/1.6.6/slf4j-log4j12-1.6.6.jar
curl -O http://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.6.6/slf4j-api-1.6.6.jar
curl -O http://repo1.maven.org/maven2/log4j/log4j/1.2.17/log4j-1.2.17.jar

popd

if (grep -E "OPTIONS=.*logging.*" start.ini) 
then
    echo "Logging already present in start.ini"
else
    echo "Adding logging to start.ini"
    echo "" >> start.ini
    echo "# Adding lib/logging to server classpath" >> start.ini
    echo "OPTIONS=logging" >> start.ini
fi

cat << EOFP > resources/jetty-logging.properties
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog
EOFP

if [ -f resources/log4j.properties ]
then
    echo "Using existing resources/log4j.properties"
else
    echo "Creating new resources/log4j.properties"
    cat << EOFL > resources/log4j.properties
# Basic Log4j Configuration with STDOUT and File logging
log4j.rootLogger=INFO, stdout, filer

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

log4j.appender.filer=org.apache.log4j.RollingFileAppender
log4j.appender.filer.layout=org.apache.log4j.PatternLayout
log4j.appender.filer.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.appender.filer.File=\${jetty.home}/logs/jetty.log
log4j.appender.filer.DatePattern='.'yyyy-MM-dd
log4j.appender.filer.MaxFileSize=10MB
log4j.appender.filer.MaxBackupIndex=4
log4j.appender.filer.append=true
EOFL
fi

This will use curl to download the JARs, setup the start.ini, and lastly the resources directory suitable for using log4j

See an error or something missing? Contribute to this documentation at Github!