How to enable logging in jetty?

2019-03-27 04:38发布

问题:

I am trying to debug my case, where simple ActiveWeb application is not running under Jetty. It behaves as if no any classes for request processing exist and return error 404.

The question is not about ActiveWeb. It is about Jetty. How to find out, that having some web application, Jetty has fond annotated class and will execute it on HTTP request?

Currently I have downloaded Jetty and it works. Unfortunately, it does no logging. Nothing is displayed in stdout or stderr and no files appear in logs subdirectory at the moment, when 404 error returns.

How to enable logging in jetty?

Documentation here http://www.eclipse.org/jetty/documentation/current/configuring-logging.html and here http://www.eclipse.org/jetty/documentation/current/configuring-jetty-request-logs.html is not clear and controversal.

For example, first page says Jetty does not use any Java loggin framework, but also requires one to select one. The second page provides some example of configuration, but does not say, where this code should be put.

回答1:

The documentation is correct, as the term "Java Logging Framework" is often associated with modern logging frameworks like java.util.logging, slf4j, logback, log4j, commons-logging, logkit, etc.

This is correct, Jetty does not use any of those.

Jetty logging predates ALL of those efforts at standardized logging frameworks. (Jetty, and its logging layer was created in 1995)

This is what Jetty logging does (and is documented at the documentation site) with regards to setup and configuration.

Default behavior:

  • If slf4j is present in your classpath, it will emit logging events to slf4j to handle using the Slf4jLog handler.
  • Fallback to StdErrLog, emitting to System.err.

To configure:

  • Specify the logging implementation you want it to use. Choices are:
    • StdErrLog
    • Slf4jLog
    • JavaUtilLog

This can be accomplished in 3 different ways

  1. Using a system property to set the logging impl
# 3 different options
-Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
-Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog
-Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.JavaUtilLog
  1. Using the jetty-logging.properties self discover/configuration found from classpath.

Example from jetty project itself:

# Configure for System.err output
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
# Configure StdErrLog to log all jetty namespace at default of WARN or above
org.eclipse.jetty.LEVEL=WARN
# Configure StdErrLog to log websocket specific namespace at DEBUG or above
org.eclipse.jetty.websocket.LEVEL=DEBUG
  1. Using code to set Log.setLog(Logger)

This is particularly useful for those using embedded-jetty

import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.StdErrLog;

Log.setLog(new StdErrLog());

Advice and Notes:

The output of your Jetty server startup will give you hints about the logging implementation it is using.

Normal / Default behavior:

2014-09-11 10:48:38.726:INFO::main: Logging initialized @378ms
2014-09-11 10:48:39.067:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/Users/joakim/Code/Jetty/distros/jetty-distribution-9.2.1.v20140609/demo-base/webapps/] at interval 1

Notice that this has either no namespace declaration, or heavily abbreviated namespace in its output. This tells me that the StdErrLog is in use.

If slf4j is in use:

10:50:18.871 [main] INFO  org.eclipse.jetty.util.log - Logging initialized @352ms
10:50:19.102 [main] INFO  o.e.j.d.p.ScanningAppProvider - Deployment monitor [file:/Users/joakim/Code/Jetty/distros/jetty-distribution-9.2.1.v20140609/demo-base/webapps/] at interval 1

This is a default Console appender output for slf4j -> logback. The overall structure here is very different that what the StdErrLog produces, so I can now tell that jetty is emitting via the Slf4jLog implementation.



标签: java Jetty