Home » Enterprise Java » jetty » Jetty Logging Configuration Example

About Ibrahim Tasyurt

Ibrahim Tasyurt
Ibrahim is a Senior Software Engineer residing in Ankara,Turkey. He holds BSc and MS degrees in Computer Engineering from Middle East Technical University(METU). Throughout his professional carrier, he has worked in Enterprise Web Application projects for public sector and telecommunications domains. Java EE, Web Services and Enterprise Application Integration are the areas he is primarily involved with.

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 Logback and 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:
--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.

java - jar start.jar

Now you can see the output similar to the following:

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:

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 directory JETTY_HOME/resources. In case you did not have one, an example file is provided below.

logback.xml

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>utf-8</charset>
            <Pattern>[%p] %c - %m%n</Pattern>
        </encoder>
    </appender>


    <logger name="org.eclipse" level="INFO"/>

  

    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
    </contextListener>

    <root level="DEBUG">
        <appender-ref ref="CONSOLE"/>
    </root>

</configuration>

When you start Jetty with the Logback configuration, you will observe a different log output in your JETTY_HOME/logs directory. 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:

<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-server</artifactId>
			<version>9.2.11.v20150529</version>
</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

package com.javacodegeeks.snippets.enterprise.enterprise.jettylogging;

import java.io.PrintStream;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.RolloverFileOutputStream;
import org.eclipse.jetty.util.log.Log;

public class JettyLoggingMain {

	public static void main(String[] args) throws Exception {

		//We are configuring a RolloverFileOutputStream with file name pattern  and appending property
		RolloverFileOutputStream os = new RolloverFileOutputStream("yyyy_mm_dd_jcglogging.log", true);
		
		//We are creating a print stream based on our RolloverFileOutputStream
		PrintStream logStream = new PrintStream(os);

		//We are redirecting system out and system error to our print stream.
		System.setOut(logStream);
		System.setErr(logStream);	

		//We are creating and starting out server on port 8080
		Server server = new Server(8080);
		server.start();
	
		//Now we are appending a line to our log 
		Log.getRootLogger().info("JCG Embedded Jetty logging started.", new Object[]{});

		server.join();

	}

}

 

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 like yyyy_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 sysout and 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:

2015-06-28 00:46:36.181:INFO::main: Logging initialized @134ms
2015-06-28 00:46:36.212:INFO:oejs.Server:main: jetty-9.2.11.v20150529
2015-06-28 00:46:36.241:INFO:oejs.ServerConnector:main: Started ServerConnector@2077d4de{HTTP/1.1}{0.0.0.0:8080}
2015-06-28 00:46:36.242:INFO:oejs.Server:main: Started @198ms
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 the logback.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:

<dependencies>

		<dependency>
			<groupId>org.eclipse.jetty</groupId>
			<artifactId>jetty-server</artifactId>
			<version>9.2.11.v20150529</version>
		</dependency>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.0.7</version>
		</dependency>
	</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”.

[INFO] org.eclipse.jetty.util.log - Logging initialized @367ms
[INFO] org.eclipse.jetty.server.Server - jetty-9.2.11.v20150529
[INFO] org.eclipse.jetty.server.ServerConnector - Started ServerConnector@25b26eee{HTTP/1.1}{0.0.0.0:8080}
[INFO] org.eclipse.jetty.server.Server - Started @435ms
[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.

8. Download the Source Code

Download
You can download the full source code of this example here: JettyLoggingExample
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of