Jetty Logging Configuration Example

In this example, we will discuss logging capabilities of Jetty. We will first enable the logging module in Jetty and configure it afterwards. As in the previous Jetty examples, we will start with standalone Jetty; thereafter we will configure logging for Embedded Jetty server too.
We are going to use Jetty v9.2.11 in this example, along with Java 8 (7 is also fine) and Apache Maven 3 as the environment. In addition to these, logging frameworks SLF4J and Logback will be utilized to configure logging in Jetty.

1. Logging in Jetty

Jetty has its own logging Logging layer which had emerged before any popular Java logging frameworks (around 1995). But Jetty does not mandate its logging layer. Other modern logging frameworks (SLF4J with Logback or Log4j or any other) can be used in Jetty logging; moreover one can wire his own logging implementation to extend Jetty’s logging capabilities.

Jetty determines its logging behavior according to the following rules:

  1. First, the value of the property org.eclipse.jetty.util.log.class is checked. If defined, the logger implementation is that class.
  2. If org.slf4j.Logger exists in the classpath, logging is decided as SLF4J.
  3. Otherwise, org.eclipse.jetty.util.log.StdErrLog is the default logging behavior.

In this example we are going to first configure Jetty with with default behavior, thereafter we will enhance it with Logbackand SLf4J.

2. Environment

In this example, following programming environment is used:

  • Java 8 (Java 7 is also OK for this example)
  • Jetty v9.x (We have used v9.2.11)
  • Apache Maven 3.x.y (for Embedded Jetty Example)
  • Eclipse Luna(for Embedded Jetty Example)

3. Enabling Logging in Jetty

Jetty 9 has a modular architecture, which means that different features (logging, SSL, SPDY, websockets etc.) are implemented as modules. These modules have to be turned on or off based on the needs.

The modules of Jetty are enabled or disabled through the start.ini file under your JETTY_HOME.

In order to activate logging module, the steps needed are below:

  1. Navigate to the JETTY_HOME
  2. Open start.ini.
  3. Add following line to start.ini as save the file:
1 --module=logging

By enabling the logging module, we have activated these files:

  • JETTY_HOME/modules/logging.mod
  • JETTY_HOME/etc/jetty-logging.xml

Further configuration will be performed via modifying these files.

Since we have not performed any logging configuration yet, Jetty will use by default org.eclipse.jetty.util.log.StdErrLog logger (the 3rd option among the ones listed above).

Before you start Jetty, check the JETTY_HOME/logs directory and see that it is empty. Now you can start jetty running the following command in your JETTY_HOME.

1 java - jar start.jar

Now you can see the output similar to the following:

1 2015-06-27 16:59:09.091:INFO::main: Redirecting stderr/stdout to /Users/ibrahim/jcgexamples/jetty/jetty-distribution-9.2.11.v20150529/logs/2015_06_27.stderrout.log

The output line means that the Jetty now logs to the file yyyy_mm_dd.stderrout ( yyyy_mm_dd is based on the current date) under JETTY_HOME/logs directory. You can see the log files in this directory. If you can see the log file under the logs directory, it means that we have successfully enabled logging module of Jetty.

4. Configuring SLF4J with Logback in Jetty

As we have mentioned earlier; it is possible to use any popular Java logging framework with Jetty. In this part, we will configure our Jetty with SLF4J and Logback.

In order to configure SLF4J with Logback, first we need to have following JAR files:

  • SLF4J API
  • logback-core
  • logback classic

After you get these JAR files, we have to copy these under your Jetty installation with these steps:

  1. Create the directory logging under JETTY_HOME.
  2. Copy these 3 JAR files to this directory (JETTY_HOME/logging).

After adding the files to our classpath, we should (although not mandatory) add a logback.xml file to the directoryJETTY_HOME/resources. In case you did not have one, an example file is provided below.

logback.xml

01 xml version="1.0" encoding="UTF-8"?>
02  
03 <configuration scan="true">
04     <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
05         <encoder>
06             <charset>utf-8charset>
07             <Pattern>[%p] %c - %m%nPattern>
08         encoder>
09     appender>
10  
11  
12     <logger name="org.eclipse" level="INFO"/>
13  
14    
15  
16     <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
17         <resetJUL>trueresetJUL>
18     contextListener>
19  
20     <root level="DEBUG">
21         <appender-ref ref="CONSOLE"/>
22     root>
23  
24 configuration>

When you start Jetty with the Logback configuration, you will observe a different log output in your JETTY_HOME/logsdirectory. You can increase verbosity of your logging output changing logging level of “org.eclipse” logger from “INFO” to “DEBUG”. When you restart your Jetty, you will see a more verbose log.

5. Changing the Location and Name of the Jetty Log Files

By default, Jetty logs to is yyyy_mm_dd.stderrout.log file under JETTY_HOME/logs. You can modify the location of the log files and the log file names. These configurations are performed via  logging.mod and jetty-logging.xml files.

In order to define a new location for the log files, The needed steps are below:

  1. Navigate to JETTY_HOME/modules directory.
  2. Open logging.mod file.
  3. Uncomment the line with the parameter with jetty.logs
  4. In order to set the new location (newlogs for example), set the parameter as jetty.logs=newlogs. Please note that the location can be either relative to your JETTY_HOME or absolute.
  5. Save the file and close.
  6. Create a directory named newlogs under your JETTY_HOME.

When you start your Jetty again, you will observe that your logs are created under JETTY_HOME/newlogs directory.

In order to change the filename of the outputs, you have to alter jetty-logging.xml file:

  1. Navigate to JETTY_HOME/etc directory.
  2. Open jetty-logging.xml file.
  3. Replace yyyy_mm_dd.stderrout.log with your preferred file name (for instance yyyy_mm_dd.javacodegeeks.log).
  4. Save and close the file.

When you restart your Jetty, you will see log files are named yyyy_mm_dd.javacodegeeks.log based on the current date.

6. Logging Configuration of Embedded Jetty

In the previous sections, we have explained how we can enable and configure logging in standalone Jetty. From now on, we are going to discuss logging configuration on Embedded Jetty. As in the standalone example, we will first start with the default logging facility of Jetty thereafter we will configure SLF4J and Logback.

6.1 Environment

As mentioned above, the programming environment is as follows:

  • Java 8 (or Java 7)
  • Jetty v9.x (v9.2.11 in this example)
  • Apache Maven 3.x.y
  • Eclipse Luna (or any convenient IDE)

6.2 Creating the Project

We will first create the Maven project in Eclipse, applying the steps below:

  1. Go to File -> New ->Other -> Maven Project
  2. Tick Create a simple project and press “Next”.
  3. Enter groupId as : com.javacodegeeks.snippets.enterprise
  4. Enter artifactId as : jetty-logging-example
  5. Press “Finish”.

6.3 Maven Dependencies

We need to add only jetty-server dependency to our pom.xml. Default logging does not require any additional dependency. The dependency entry looks as follows in the pom:

1 <dependency>
2             <groupId>org.eclipse.jettygroupId>
3             <artifactId>jetty-serverartifactId>
4             <version>9.2.11.v20150529version>
5 dependency>

For the SLF4J, Logback example we are going to need additional dependencies (logback-classic). We will address this in the related section. In the source code of this example, you can simply comment out  additional dependencies.

6.4 Default Logging Example

After adding configuring our pom, we are now ready to code. In order to keep things simple in this example, we are going to create our Embedded Jetty server in our main class.

Our main class is JettyLoggingMain under the package com.javacodegeeks.snippets.enterprise.enterprise.jettylogging.

The source code of JettyLoggingMain decorated with the descriptive comment lines is as follows:

JettyLoggingMain.java

01 package com.javacodegeeks.snippets.enterprise.enterprise.jettylogging;
02  
03 import java.io.PrintStream;
04  
05 import org.eclipse.jetty.server.Server;
06 import org.eclipse.jetty.util.RolloverFileOutputStream;
07 import org.eclipse.jetty.util.log.Log;
08  
09 public class JettyLoggingMain {
10  
11     public static void main(String[] args) throws Exception {
12  
13         //We are configuring a RolloverFileOutputStream with file name pattern  and appending property
14         RolloverFileOutputStream os = newRolloverFileOutputStream("yyyy_mm_dd_jcglogging.log"true);
15          
16         //We are creating a print stream based on our RolloverFileOutputStream
17         PrintStream logStream = new PrintStream(os);
18  
19         //We are redirecting system out and system error to our print stream.
20         System.setOut(logStream);
21         System.setErr(logStream);  
22  
23         //We are creating and starting out server on port 8080
24         Server server = new Server(8080);
25         server.start();
26      
27         //Now we are appending a line to our log
28         Log.getRootLogger().info("JCG Embedded Jetty logging started."new Object[]{});
29  
30         server.join();
31  
32     }
33  
34 }

 

In the code, we have first created a RolloverFileOutputStream object . We created this object with two parameters.

First one is the filename pattern. In order to specify date in the log file, this filename has to include a pattern likeyyyy_mm_dd. Otherwise, Jetty will simply create a file with the name specified(without any date information). In this example we have named this pattern as “yyyy_mm_dd_jcglogging.log”.

The second parameter is append. When set to true, the logger will append to an existing file for each restart. Otherwise, it will create a new file (with a timestamp information) at each restart. In this example, we set the parameter as “true”.

Then we have created a PrintStream and provided our RolloverFileOutputStream as the argument. We have directed sysoutand syserr to this PrintStream.

Now our logging configuration is complete. In the following lines of code, we start our Embedded Server and append a simple log line.

When we run our main class, our server starts at port 8080. Our logging file (2015_06_28_jcglogging.log) is created at our project directory. The content looks like as follows:

1 2015-06-28 00:46:36.181:INFO::main: Logging initialized @134ms
2 2015-06-28 00:46:36.212:INFO:oejs.Server:main: jetty-9.2.11.v20150529
3 2015-06-28 00:46:36.241:INFO:oejs.ServerConnector:main: Started ServerConnector@2077d4de{HTTP/1.1}{0.0.0.0:8080}
4 2015-06-28 00:46:36.242:INFO:oejs.Server:main: Started @198ms
5 2015-06-28 00:46:36.242:INFO::main: JCG Embedded Jetty logging started.

6.5 SLF4J and Logback Example

In the first part, we have created an Embedded Jetty with default configuration. In order to configure SLF4J and Logback, you have to apply two steps:

  1. Add SLF4J and Logback dependencies to your pom.xml (in addition to Jetty server dependencies).
  2. Add a logback.xml file to your classpath.(This step is optional but needed for detailed configuration). You can copy thelogback.xml you have used in the standalone example under “src/main/resources”.

The dependency to be added is:

  • ch.qos.logback:logback-classic (v1.0.7)

This single dependency also fetches logback-core and SLF4J form the Maven repository. After adding this dependency, your dependencies section in the pom.xml looks as follows:

01 <dependencies>
02  
03         <dependency>
04             <groupId>org.eclipse.jettygroupId>
05             <artifactId>jetty-serverartifactId>
06             <version>9.2.11.v20150529version>
07         dependency>
08  
09         <dependency>
10             <groupId>ch.qos.logbackgroupId>
11             <artifactId>logback-classicartifactId>
12             <version>1.0.7version>
13         dependency>
14     dependencies>

For the Logback example, you don’t have to modify any single line of code. As we have mentioned above, when Jetty finds SLF4J in the classpath, it will automatically switches to SLF4J(case 2). When you run the same main class of the previous example, you will see the SLF4J log in “yyyy_mm_dd_jcglogging.log”.

1 [INFO] org.eclipse.jetty.util.log - Logging initialized @367ms
2 [INFO] org.eclipse.jetty.server.Server - jetty-9.2.11.v20150529
3 [INFO] org.eclipse.jetty.server.ServerConnector - Started ServerConnector@25b26eee{HTTP/1.1}{0.0.0.0:8080}
4 [INFO] org.eclipse.jetty.server.Server - Started @435ms
5 [INFO] org.eclipse.jetty.util.log - JCG Embedded Jetty logging started.

Now our example with Embedded Jetty is complete.

7. Conclusion

In this post we have first configured a standalone Jetty server for logging. We have started with enabling logging in Jetty. Then we have configured Jetty both for default Jetty logging and SLF4-Logback logging. Thereafter we have repeated same configuration programmatically for an embedded Jetty Server.

For further configuration with other parameters and logging frameworks, you can refer to the official Jetty documentation on logging.

你可能感兴趣的:(jetty,开发工具,java)