Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable logging in jetty?

Tags:

java

jetty

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.

like image 604
Suzan Cioc Avatar asked Sep 11 '14 11:09

Suzan Cioc


People also ask

How do I turn off jetty logging?

jetty. LEVEL", "OFF"); This worked for me, but just make sure to call it before starting the server.

Does jetty use Log4j?

Jetty is now configured to log using the Log4j framework. A standard Log4j configuration file is located in ${jetty. base}/resources/log4j. xml .


2 Answers

Updated (July 2020) answer for Jetty 10 and newer

Starting with Jetty 10.0.0, there is no longer a "Jetty Logging" facade.
The Jetty project has moved to using SLF4J 2.0 only.

The original answer for Jetty 9 and older

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.

like image 154
Joakim Erdfelt Avatar answered Sep 28 '22 20:09

Joakim Erdfelt


In my case, I've solved it by following the recommendations in the documentation page (Default Logging with Jetty’s StdErrLog) and adding the following line in the start.ini file: --module=console-capture

I know this is only one way of solving your problem, but it worked out pretty well for me. After restarting the service, I could already see a log file in the logs subdirectory.

like image 35
Andorkan Avatar answered Sep 28 '22 18:09

Andorkan