Version: 9.3.16.v20170120 |

Example: Logging with Logback
Previous | Chapter 11. Jetty Logging Home | Next

##### 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 for sponsored feature development

## Example: Logging with Logback

It is possible to have the Jetty Server logging configured so that Logback controls the output of logging events produced by Jetty. This is accomplished by configuring Jetty for logging to `Logback`, which uses Slf4j and the Logback Implementation for Slf4j.

A convenient replacement `logging` module has been created to bootstrap the `${jetty.base}` directory for logging with logback.

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

    [modules]$ curl -O https://raw.githubusercontent.com/jetty-project/logging-modules/master/logback/logging.mod
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100   742  100   742    0     0   2196      0 --:--:-- --:--:-- --:--:--  2201
    [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/ch/qos/logback/logback-core/1.0.7/logback-core-1.0.7.jar to lib/logging/logback-core-1.0.7.jar
    DOWNLOAD: http://central.maven.org/maven2/ch/qos/logback/logback-classic/1.0.7/logback-classic-1.0.7.jar to lib/logging/logback-classic-1.0.7.jar
    DOWNLOAD: https://raw.githubusercontent.com/jetty-project/logging-modules/master/logback/logback.xml to resources/logback.xml
    DOWNLOAD: https://raw.githubusercontent.com/jetty-project/logging-modules/master/logback/jetty-logging.properties to resources/jetty-logging.properties

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

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) to the `${jetty.base}/lib/logging/` directory: slf4j-api, logback-core, and logback-classic.
   4. Required configuration files are downloaded (if not present already) to the `${jetty.base}/lib/resources/` directory.: `jetty-logging.properties`, and `logback.xml`.

At this point the Jetty `mybase` is configured so that the Jetty server itself will log using Logback, using the Logback configuration found in `mybase/resources/logback.xml`

The server classpath can be verified 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 logback-classic.jar, making all Jetty + Slf4j + Logback events emitted by the Jetty server go to Logback for routing (to console, file, syslog, etc…​).

Previous | Top | Next
Example: Logging with Java’s java.util.logging via JavaUtilLog | Home | Example: Capturing Multiple Logging Frameworks with Slf4j

See an error or something missing? Contribute to this documentation at Github!(Generated: 2017-01-20)