![]() Version: 9.3.28.v20191105 |
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
This page describes how to configure Jetty for capturing multiple logging frameworks logging events into a single logging implementation handled by Slf4j.
A single logging solution can be configured for the variety of logging libraries available in common use when using Slf4j. With careful setup, all of the following logging APIs can be supported at the same time, with a single configuration file to control the output of events produced.
Logging APIs that Slf4j supports:
java.util.logging
)To accomplish this configuration a single underlying logging framework should first be chosen. This decision guides the rest of the choices about jar files to place on the server classpath.
Caution
There MUST NOT be multiple underlying logging frameworks on the classpath. If there are, the Slf4j framework fails to load.
Note
Some third party libraries provide their own implementations of common logging APIs; be careful not to accidentally include an underlying logging framework. For example, if you are using SpringSource you likely have a
com.springsource.org.apache.log4j.jar
along with alog4j.jar
, which have the same classes in them. In this example, use thecom.springsource.org.apache.log4j.jar
version and exclude thelog4j.jar
, as the SpringSource version includes extra metadata suitable for using SpringSource.
Table 11.1. Slf4j Logging Grid
Logging API | Slf4j Binding Jar | Slf4j Adapter Jar | Underlying Logging Framework |
---|---|---|---|
Logback API | n/a | logback-classic.jar | logback-core.jar |
Log4j | slf4j-log4j12.jar | log4j.jar | |
JDK 1.4 Logging | slf4j-jdk14.jar | (Core Java Classlib) | |
Commons Logging | slf4j-jcl.jar | commons-logging.jar |
The following sections use Logback as the underlying Logging framework.
This requires using logback-classic.jar
and logback-core.jar
, and excluding any other Slf4j adapter JAR or underlying logging framework.
It also requires including the other Slf4j binding JARs in the classpath, along with some special initialization for java.util.logging
.
A convenient replacement logging
module has been created to bootstrap the ${jetty.base}
directory for capturing all Jetty server logging from multiple logging frameworks into a single logging output file managed by logback.
[mybase]$ mkdir modules [mybase]$ cd modules [modules]$ curl -O https://raw.githubusercontent.com/jetty-project/logging-modules/master/capture-all/logging.mod % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1293 100 1293 0 0 3693 0 --:--:-- --:--:-- --:--:-- 3694 [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/log4j-over-slf4j/1.6.6/log4j-over-slf4j-1.6.6.jar to lib/logging/log4j-over-slf4j-1.6.6.jar DOWNLOAD: http://central.maven.org/maven2/org/slf4j/jul-to-slf4j/1.6.6/jul-to-slf4j-1.6.6.jar to lib/logging/jul-to-slf4j-1.6.6.jar DOWNLOAD: http://central.maven.org/maven2/org/slf4j/jcl-over-slf4j/1.6.6/jcl-over-slf4j-1.6.6.jar to lib/logging/jcl-over-slf4j-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/capture-all/logback.xml to resources/logback.xml DOWNLOAD: https://raw.githubusercontent.com/jetty-project/logging-modules/master/capture-all/jetty-logging.properties to resources/jetty-logging.properties DOWNLOAD: https://raw.githubusercontent.com/jetty-project/logging-modules/master/capture-all/jetty-logging.xml to etc/jetty-logging.xml INFO: resources initialised transitively INFO: resources enabled in ${jetty.base}/start.ini [mybase]$ java -jar /opt/jetty-dist/start.jar
The replacement logging.mod
performs a number of tasks.
mybase
is a ${jetty.base}
directory./opt/jetty-dist/
and becomes the ${jetty.home}
directory for this demonstration.curl
command downloads the replacement logging.mod
and puts it into the ${jetty.base}/modules/
directory for use by mybase
only.The start.jar --add-to-start=logging
command performs a number of steps to make the logging module available to the ${jetty.base}
configuration.
--module=logging
command is added to the ${jetty.base}/start.ini
configuration.${jetty.base}
directories are created: ${jetty.base}/logs
and ${jetty.base}/resources
.Required libraries are downloaded (if not present already) to the the ${jetty.base}/lib/logging/
directory:
slf4j-api.jar
- API jar for Slf4j (used by most of the rest of the jars)log4j-over-slf4j.jar
- Slf4j jar that captures all log4j emitted logging eventsjul-to-slf4j.jar
- Slf4j jar that captures all java.util.logging
eventsjcl-over-slf4j.jar
- Slf4j jar that captures all commons-logging
eventslogback-classic.jar
- the Slf4j adapter jar that routes all of the captured logging events to logback itself.logback-core.jar
- the logback implementation jar, that handles all of the filtering and output of the logging events.${jetty.base}/resources/
directory: jetty-logging.properties
, and logback.xml
java.util.logging
initialization commands are downloaded (if not present already) to the ${jetty.base}/etc/
directory: jetty-logging.xml
At this point the Jetty mybase
is configured so that the jetty server itself will log using slf4j, and all other logging events from other Jetty server components (such as database drivers, security layers, jsp, mail, and other 3rd party server components) are routed to logback for filtering and output.
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 various slf4j bridge jars are acting on behalf of log4j, java.util.logging
, and commons-logging
, routing all of the logging events to logback (a Slf4j adapter) for routing (to console, file, etc…).