Jetty Logo
Version: 9.2.2-SNAPSHOT
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 via JavaUtilLog

It is possible to have the Jetty Server logging configured so that java.util.logging controls the output of logging events produced by Jetty.

This example demonstrates how to configuring Jetty for logging to java.util.logging via Jetty's own JavaUtilLog implementation.

Important

While this is a valid setup, the Jetty project recommends always using the slf4j to java.util.logging configuration for memory and performance reasons. (this naive implementation is very non-performant and is not guaranteed to exist in the future)

Quick Setup of java.util.logging (native) Logging using Jetty 9.2.1+

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

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

[modules]$ curl -O https://raw.githubusercontent.com/jetty-project/logging-modules/master/java.util.logging-native/logging.mod
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   623  100   623    0     0   1879      0 --:--:-- --:--:-- --:--:--  1876
[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: https://raw.githubusercontent.com/jetty-project/logging-modules/master/java.util.logging-native/jetty-logging.xml to etc/jetty-logging.xml
DOWNLOAD: https://raw.githubusercontent.com/jetty-project/logging-modules/master/java.util.logging-native/logging.properties to resources/logging.properties
DOWNLOAD: https://raw.githubusercontent.com/jetty-project/logging-modules/master/java.util.logging-native/jetty-logging.properties to resources/jetty-logging.properties
INFO: resources       initialised transitively
INFO: resources       enabled in     ${jetty.base}/${jetty.base}

[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 configuration files are downloaded (if not present already): jetty-logging.properties, and logging.properties

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

    4. Required java.util.logging initialization commands are downloaded (if not present already): jetty-logging.xml

      The xml file is put in the ${jetty.base}/etc/ directory.

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

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

In essence, Jetty is now configured to use org.eclipse.jetty.util.log.JavaUtilLog, which emit its own logging events to java.util.logging. Making all Jetty + java.util.logging events emitted by the Jetty server go to java.util.logging for routing (to console, file, etc...)

If you have any custom java.util.logging handlers that you want to use, put the implementation jar in your ${jetty.base}/lib/logging/ directory and reference them in the ${jetty.base}/resources/logging.properties file.

Note

java.util.logging is configured via the ${jetty.base}/resources/logging.properties file during a valid startup of Jetty. This means that if there is any startup errors that occur before java.util.logging is configured, they will likely be lost and/or not routed through your configuration. (Other logging frameworks are more reliable in that they always initialize and configure on first use, unlike java.util.logging)

While it is possible to configure java.util.logging sooner, even at JVM startup, the example demonstrated here does not show this technique. For more information consult the official java.util.logging.LogManager javadoc documentation from Oracle.

See an error or something missing? Contribute to this documentation at Github!(Generated: 2014-09-14T01:00:29-07:00)