Java 如何在码头启用登录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25786592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:07:40  来源:igfitidea点击:

How to enable logging in jetty?

javajetty

提问by Suzan Cioc

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.

我正在尝试调试我的案例,其中简单的 ActiveWeb 应用程序未在 Jetty 下运行。它的行为就像不存在任何用于请求处理的类并返回错误 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?

问题不在于 ActiveWeb。这是关于码头的。如何发现,有一些 Web 应用程序,Jetty 喜欢带注释的类并将在 HTTP 请求上执行它?

Currently I have downloaded Jetty and it works. Unfortunately, it does no logging. Nothing is displayed in stdoutor stderrand no files appear in logssubdirectory at the moment, when 404 error returns.

目前我已经下载了 Jetty 并且它可以工作。不幸的是,它没有记录。当 404 错误返回时,stdoutor 中stderr没有显示任何内容,并且logs此时子目录中没有任何文件出现。

How to enable logging in jetty?

如何在码头启用登录?

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

这里的文档http://www.eclipse.org/jetty/documentation/current/configuring-logging.html和这里的文档http://www.eclipse.org/jetty/documentation/current/configuring-jetty-request-logs.html不清楚和有争议。

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.

例如,第一页说Jetty没有使用任何Java登录框架,但也需要一个选择一个。第二页提供了一些配置示例,但没有说明这段代码应该放在哪里。

回答by Joakim Erdfelt

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.

文档是正确的,因为术语“Java 日志框架”通常与现代日志框架相关联,如 java.util.logging、slf4j、logback、log4j、commons-logging、logkit 等。

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

这是正确的,Jetty 不使用任何这些。

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

Jetty 日志早于标准化日志框架的所有这些努力。(Jetty,其日志层创建于 1995 年)

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

这就是码头记录不会(和被记录在文档站点)与关于设置和配置。

Default behavior:

默认行为:

  • If slf4jis present in your classpath, it will emit logging events to slf4j to handle using the Slf4jLoghandler.
  • Fallback to StdErrLog, emitting to System.err.
  • 如果slf4j存在于您的类路径中,它将向 slf4j 发出日志事件以使用Slf4jLog处理程序进行处理。
  • 回退到StdErrLog,发送到 System.err。

To configure:

配置:

This can be accomplished in 3 different ways

这可以通过 3 种不同的方式完成

  1. Using a system property to set the logging impl
  1. 使用系统属性设置日志记录实现
# 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.propertiesself discover/configuration found from classpath.
  1. 使用jetty-logging.properties从类路径中找到的自我发现/配置。

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)
  1. 使用代码设置 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.

Jetty 服务器启动的输出将为您提供有关它正在使用的日志记录实现的提示。

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 StdErrLogis in use.

请注意,它要么没有命名空间声明,要么在其输出中使用了大量缩写的命名空间。这告诉我StdErrLog正在使用。

If slf4j is in use:

如果正在使用 slf4j:

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 appenderoutput for slf4j-> logback. The overall structure here is very different that what the StdErrLogproduces, so I can now tell that jetty is emitting via the Slf4jLogimplementation.

这是->的默认控制台附加程序输出。这里的整体结构与产生的非常不同,所以我现在可以知道码头是通过实现发出的。slf4jlogbackStdErrLogSlf4jLog

回答by Andorkan

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

就我而言,我已经按照文档页面中的建议(使用 Jetty 的 StdErrLog 进行默认日志记录)并在 start.ini 文件中添加以下行来解决它:--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.

我知道这只是解决您问题的一种方法,但对我来说效果很好。重新启动服务后,我已经可以在 logs 子目录中看到一个日志文件。