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 Logback

This page describes how to Configure Jetty for Logging with Logback.

The basic configuration for Logging with Logback is to use Slf4j and the Logback Implementation for Slf4j.

The Required JARs

There will be 3 JARs you will need to download

logback-classic-1.0.7.jar

This is the underlying logging framework and adaption layer for Slf4j

logback-core-1.0.7.jar

The common implementation classes for the Logback project.

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

[jetty-distribution-9.0.0.v20130308]$ ls -l lib/logging/
total 632
-rw-rw-r-- 1 jetty jetty 251679 Mar 12 14:46 logback-classic-1.0.7.jar
-rw-rw-r-- 1 jetty jetty 364399 Mar 12 14:46 logback-core-1.0.7.jar
-rw-rw-r-- 1 jetty jetty  26176 Mar 12 14:46 slf4j-api-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)

# Adding lib/logging to server classpath
OPTIONS=logging

Now verify that the 3 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.0.7 | ${jetty.home}/lib/logging/logback-classic-1.0.7.jar
19:                1.0.7 | ${jetty.home}/lib/logging/logback-core-1.0.7.jar
20:                1.6.6 | ${jetty.home}/lib/logging/slf4j-api-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 Logback

You'll need a ${jetty.home}/resources/logback.xml file to define what you want Logback to do. It will be automatically located and loaded on Logging startup.

Example ${jetty.home}/resources/logback.xml file

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
  Example LOGBACK Configuration File
  http://logback.qos.ch/manual/configuration.html
  -->
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>\${jetty.home}/logs/jetty.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>jetty_%d{yyyy-MM-dd}.log</fileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="info">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
  </root>
</configuration>

Scripted Installation of Logback

#!/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/ch/qos/logback/logback-classic/1.0.7/logback-classic-1.0.7.jar
curl -O http://repo1.maven.org/maven2/ch/qos/logback/logback-core/1.0.7/logback-core-1.0.7.jar
curl -O http://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.6.6/slf4j-api-1.6.6.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/logback.xml ]
then
    echo "Using existing resources/logback.xml"
else
    echo "Creating new resources/logback.xml"
    cat << EOFL > resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 
  Example LOGBACK Configuration File
  http://logback.qos.ch/manual/configuration.html
  -->
<configuration>
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>

  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>\${jetty.home}/logs/jetty.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>jetty_%d{yyyy-MM-dd}.log</fileNamePattern>
      <maxHistory>30</maxHistory>
    </rollingPolicy>

    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>

  <root level="info">
    <appender-ref ref="STDOUT" />
    <appender-ref ref="FILE" />
  </root>
</configuration>
EOFL
fi

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