`
jerome_wang
  • 浏览: 157135 次
  • 性别: Icon_minigender_1
  • 来自: 云南
社区版块
存档分类
最新评论

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:

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-8</charset>
07             <Pattern>[%p] %c - %m%n</Pattern>
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>true</resetJUL>
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.jetty</groupId>
3             <artifactId>jetty-server</artifactId>
4             <version>9.2.11.v20150529</version>
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.jetty</groupId>
05             <artifactId>jetty-server</artifactId>
06             <version>9.2.11.v20150529</version>
07         </dependency>
08  
09         <dependency>
10             <groupId>ch.qos.logback</groupId>
11             <artifactId>logback-classic</artifactId>
12             <version>1.0.7</version>
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 源代码及example

    标题中的"jetty源代码及example"指的是包含了Jetty服务器的源代码以及相关的示例项目,这对于深入理解Jetty的工作原理、自定义配置以及学习Servlet编程具有很高的价值。 在描述中提到,"jetty源代码,是研究servlet...

    grails-jetty-example:使用Jetty而不是Tomcat的示例Grails 4 Web应用程序

    Grails Jetty示例应用程序这是... 使用命令docker build -t jetty-example:0.1构建Docker镜像。 启动Docker容器应用程序docker run -it -p 8080:8080 -p 8443:8443 jetty-example:0.1 。 打开浏览器窗口,然后导航到 。

    jetty 6 指南书

    - **Jetty XML Configuration语法**:讲解XML配置文件的语法规则。 - **org.mortbay.xml.XmlConfiguration**:介绍使用该类来读取和应用XML配置。 5. **部署Web应用程序** - **静态部署**:将WAR或WEB-INF目录...

    Jetty中文手册

    Jetty XML语法(Syntax)–Jetty IOC Configuration Jetty XML用法–Using and Combining Jetty Configurations 配置文件 jetty.xml–Server configuration jetty-web.xml–Web App configuration jetty-env.xml–...

    jetty 8及依赖包

    此外,Jetty 8的依赖包中可能包含了各种库,如SLF4J(Simple Logging Facade for Java)用于日志记录,或者Jetty自身的服务器配置和管理工具。这些库是Jetty正常运行所必需的,它们提供了诸如会话管理、安全控制、...

    jetty相关的全部jar包

    jetty-security-9.4.8.v20171121.jar,jetty-io-9.4.8.v20171121.jar,jetty-continuation-9.4.8.v20171121.jar,jetty-client-9.4.8.v20171121.jar,jetty-jmx-9.4.8.v20171121.jar,jetty-plus-9.4.8.v20171121....

    maven集成jetty所需jar包maven-jetty-plugin,多版本

    在Java开发领域,Maven和Jetty是两个非常重要的工具。Maven是一个项目管理工具,它可以帮助开发者管理和构建Java项目,而Jetty则是一个轻量级的嵌入式Servlet容器,常用于快速开发、测试以及部署Web应用。本文将详细...

    jetty嵌入式服务器必须的jar包

    jetty嵌入式服务器开发所必须的jar包,本人使用jetty版本为6.1.3,jar包总数为9个,来自jetty:commons-el-1.0.jar,jasper-compiler-5.5.15,jasper-compiler-jdt-5.5.15.jar,jasper-runtime-5.5.15.jar,jetty-...

    maven-jetty-plugin

    ** Maven Jetty Plugin 知识点详解 ** Maven Jetty Plugin是一款强大的工具,它将Jetty服务器集成到了Maven的构建流程中。这个插件允许开发者在开发过程中快速、便捷地运行和测试Java Web应用程序,而无需进行完整...

    jetty各个版本下载

    Jetty是一款轻量级、高性能的Java Web服务器和Servlet容器,它被广泛应用于各种规模的项目,从小型的个人项目到大型的企业级应用。Jetty以其高效、稳定和易于集成的特点,深受开发者喜爱。在本篇文章中,我们将深入...

    实战 Jetty--让你快速速学会jetty

    new XmlConfiguration(new FileInputStream("jetty.xml")).configure(server); } catch (Exception e) { e.printStackTrace(); } try { server.start(); } catch (Exception e) { e.printStackTrace(); } ...

    jetty修改js不用重启项目的方法

    Jetty是一个轻量级、高性能的Java Web服务器和Servlet容器,它允许开发者快速地部署和管理Web应用程序。在开发过程中,频繁地修改JavaScript文件并希望即时看到效果是常见的需求,而每次修改后都需要重启Jetty服务...

    eclipse jetty插件安装(离线版)

    Eclipse Jetty插件是开发Java Web应用时非常实用的工具,它允许开发者在Eclipse集成开发环境中直接启动和测试Jetty服务器,而无需通过命令行或其他方式。本篇文章将详细讲解如何离线安装Eclipse Jetty插件,并介绍其...

    maven项目下用 jetty 启动热部署

    当我们使用Maven构建Java Web项目时,结合Jetty服务器,可以实现项目的热部署功能,即在开发环境中修改代码后无需重新启动服务器,改动就能实时生效,极大地提高了开发效率。 在Maven项目中启用Jetty热部署,主要...

    jetty 适合jdk1.8用的服务器

    Jetty是一款开源、轻量级的Web服务器和Servlet容器,被广泛用于开发、测试和部署Java Web应用程序。相较于Apache Tomcat,Jetty以其简洁的架构、高性能和低内存占用而受到开发者青睐。在选择Jetty时,必须考虑到与...

    jetty-all.jar

    Jetty-all.jar是一个集合了众多Jetty组件的综合性JAR文件,它的主要目的是为了方便开发者在项目中快速引入和使用Jetty服务器。Jetty是一个开源的HTTP服务器和Servlet容器,它以其轻量级、高性能和易用性而受到广泛...

    jetty-6.1.26.zip

    Jetty是一款轻量级、高性能的Java Web服务器和Servlet容器,与Tomcat相似,它为开发和部署Web应用程序提供了一种高效的选择。Jetty在设计上注重灵活性和可扩展性,使得它在处理HTTP协议、WebSocket协议以及部署各种...

    Jetty权威指南.pdf

    `org.mortbay.xml.XmlConfiguration`类提供了读取和解析Jetty配置文件的能力。用户可以通过这个类来加载配置文件,并将配置信息转换成Java对象模型,方便后续的配置管理和使用。 #### 五、在Jetty中部署Web应用程序...

    jetty 9.4.9

    Jetty 9.4.9 是一个开源的Java Web服务器和Servlet容器,以其轻量级、高效和灵活性而受到开发者的欢迎。这个版本是Jetty项目的一个重要里程碑,提供了许多性能改进和新特性。在深入探讨之前,让我们先了解一些基本...

    jetty嵌入项目实战

    - 通过配置Jetty的logging.properties文件或在代码中设置日志级别,可以控制日志的详细程度。 6. **安全性与会话管理** - Jetty支持基本认证、HTTPS等安全机制,可以通过`org.eclipse.jetty.security`包实现。 -...

Global site tag (gtag.js) - Google Analytics