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 Java's java.util.logging

This page describes how to Configure Jetty for Logging with Java's java.util.logging.

The basic configuration for Logging with java.util.logging is to use Slf4j and the Slf4j binding layer for java.util.logging.

The Required JARs

There will be 2 JARs you will need to download

slf4j-jdk14-1.6.6.jar

This is the Slf4j binding layer for JDK 1.4 logging (also known as java.util.logging)

slf4j-api-1.6.6.jar

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

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 2 files you downloaded there.

[jetty-distribution-9.0.0.v20130308]$ ls -l lib/logging/
total 40
-rw-rw-r-- 1 jetty jetty 26176 Mar 11 16:38 slf4j-api-1.6.6.jar
-rw-rw-r-- 1 jetty jetty  8845 Mar 11 16:38 slf4j-jdk14-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 the following entries. (Add them to the end if missing)

# Configure java.util.logging from resources/logging.properties
etc/jetty-jdk-logging.xml
# Adding lib/logging to server classpath
OPTIONS=logging

Now verify that the 2 jars will be loaded by 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.6.6 | ${jetty.home}/lib/logging/slf4j-api-1.6.6.jar
19:                1.6.6 | ${jetty.home}/lib/logging/slf4j-jdk14-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 java.util.logging

You'll need a ${jetty.home}/resources/logging.properties file to define what you want java.util.logging to do, and finally a way to have java.util.logging use this configuration.

Example ${jetty.home}/resources/logging.properties file

handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.pattern=logs/jetty.log

# Write 10MB before rotating this file
java.util.logging.FileHandler.limit=10000000

# Number of rotating files to be used
java.util.logging.FileHandler.count=4
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter

.level=INFO

Finally, you'll want a way for this java.util.logging configuration to take effect on server start.

Contents of etc/jetty-jdk-logging.xml file that we added earlier to the start.ini

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Call id="logMgr" class="java.util.logging.LogManager" name="getLogManager">
    <Call name="readConfiguration">
      <Arg class="java.io.InputStream">
        <New class="java.io.FileInputStream">
          <Arg class="java.lang.String"><Property name="jetty.home" default="."/>/resources/logging.properties</Arg>
        </New>
      </Arg>
    </Call>
  </Call>
</Configure>

Scripted Installation of java.util.logging

#!/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-jdk14/1.6.6/slf4j-jdk14-1.6.6.jar
curl -O http://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.6.6/slf4j-api-1.6.6.jar

popd

echo "Creating etc/jetty-jdk-logging.xml"
cat << EOFX > etc/jetty-jdk-logging.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Call id="logMgr" class="java.util.logging.LogManager" name="getLogManager">
    <Call name="readConfiguration">
      <Arg class="java.io.InputStream">
        <New class="java.io.FileInputStream">
          <Arg class="java.lang.String"><Property name="jetty.home" default="."/>/resources/logging.properties</Arg>
        </New>
      </Arg>
    </Call>
  </Call>
</Configure>
EOFX

if (grep -E "OPTIONS=.*logging.*" start.ini) 
then
    echo "Logging already present in start.ini"
else
    echo "Adding logging to start.ini"
    cat << EOFI >> start.ini
# Configure java.util.logging from resources/logging.properties
etc/jetty-jdk-logging.xml
# Adding lib/logging to server classpath
OPTIONS=logging
EOFI
fi

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

echo "Rewriting resources/logging.properties"
cat << EOFJ > resources/logging.properties
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter

java.util.logging.FileHandler.level=INFO
java.util.logging.FileHandler.pattern=logs/jetty.log

# Write 10MB before rotating this file
java.util.logging.FileHandler.limit=10000000

# Number of rotating files to be used
java.util.logging.FileHandler.count=4
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter

.level=INFO
EOFJ

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