How to enable logging in jetty?

后端 未结 2 1812
清酒与你
清酒与你 2021-02-02 17:28

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.

相关标签:
2条回答
  • 2021-02-02 17:31

    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.

    0 讨论(0)
  • 2021-02-02 17:31

    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.

    0 讨论(0)
提交回复
热议问题