Jetty Logo
Version: 9.2.3.v20140905
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

It is possible to have the Jetty Server logging configured so that Log4j controls the output of logging events produced by Jetty. This is accomplished by configuring Jetty for logging to Apache Log4j via Slf4j and the Slf4j binding layer for Log4j.

Quick Setup of Log4j Logging using Jetty 9.2.1+

A convenient replacement logging module has been created to bootstrap your ${jetty.base} directory for logging with log4j.

[mybase]$ mkdir modules
[mybase]$ cd modules

[modules]$ curl -O https://raw.githubusercontent.com/jetty-project/logging-modules/master/log4j-1.2/logging.mod
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   720  100   720    0     0   2188      0 --:--:-- --:--:-- --:--:--  2188
[modules]$ cd ..

[mybase]$ java -jar /opt/jetty-dist/start.jar --add-to-start=logging
INFO: logging         initialised in ${jetty.base}/start.ini (appended)
MKDIR: ${jetty.base}/logs
DOWNLOAD: http://central.maven.org/maven2/org/slf4j/slf4j-api/1.6.6/slf4j-api-1.6.6.jar to lib/logging/slf4j-api-1.6.6.jar
DOWNLOAD: http://central.maven.org/maven2/org/slf4j/slf4j-log4j12/1.6.6/slf4j-log4j12-1.6.6.jar to lib/logging/slf4j-log4j12-1.6.6.jar
DOWNLOAD: http://central.maven.org/maven2/log4j/log4j/1.2.17/log4j-1.2.17.jar to lib/logging/log4j-1.2.17.jar
DOWNLOAD: https://raw.githubusercontent.com/jetty-project/logging-modules/master/log4j-1.2/log4j.properties to resources/log4j.properties
DOWNLOAD: https://raw.githubusercontent.com/jetty-project/logging-modules/master/log4j-1.2/jetty-logging.properties to resources/jetty-logging.properties
INFO: resources       initialised transitively
INFO: resources       enabled in     ${jetty.base}/start.ini

[mybase]$ java -jar /opt/jetty-dist/start.jar

That's Cool! But what just happened?

The replacement logging.mod performs a number of tasks.

  1. mybase is a ${jetty.base} directory

  2. The jetty-distribution is unpacked (and untouched) into /opt/jetty-dist/ and becomes the ${jetty.home} directory for this demonstration.

  3. The curl command downloads the replacement logging.mod and puts it into the ${jetty.base}/modules/ directory for use by mybase only.

  4. The start.jar --add-to-start=logging command performs a number of steps to make the logging module available to the ${jetty.base} configuration.

    1. The --module=logging command is added to the ${jetty.base}/start.ini configuration

    2. Required ${jetty.base} directories are created: ${jetty.base}/logs and ${jetty.base}/resources

    3. Required libraries are downloaded (if not present already): slf4j-api, slf4j-log4j, and log4j itself.

      The libraries are put in the ${jetty.base}/lib/logging/ directory.

    4. Required configuration files are downloaded (if not present already): jetty-logging.properties, and log4j.properties

      The configuration files are put in the ${jetty.base}/resources/ directory.

  5. At this point you have your mybase configured so that the jetty server itself will log using log4j, using the log4j configuration found in mybase/resources/log4j.properties

You can verify the server classpath by using the start.jar --list-config command.

In essence, Jetty is now configured to emit its own logging events to slf4j, and slf4j itself is using the static log binder found in slf4j-log4j12.jar. Making all Jetty + Slf4j + Log4j events emitted by the Jetty server go to Log4j for routing (to console, file, syslog, etc...)

See an error or something missing? Contribute to this documentation at Github!(Generated: 2014-11-29T01:00:27-08:00)