Jersey 2.23:无法从 web.xml 启用 LoggingFeature

Jersey 2.23 : Not able to enable LoggingFeature from web.xml

我正在尝试在 Jersey 运行 Tomcat 中启用 HTTP request/response 登录。我的应用程序基于 web.xml,在 ResourceConfig class 上没有应用程序。

这就是我尝试从 web.xml 启用 LoggingFeature 的方法:

<init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.logging.LoggingFeature</param-value>
    </init-param>

<init-param>
    <param-name>org.glassfish.jersey.logging.LoggingFeature.LOGGING_FEATURE_LOGGER_LEVEL</param-name>
    <param-value>java.util.logging.Level.FINEST</param-value>
</init-param>

<init-param>
    <param-name>org.glassfish.jersey.logging.LoggingFeature.LOGGING_FEATURE_LOGGER_NAME</param-name>
    <param-value>MyLoggerName</param-value>
</init-param>

但我在 catalina.out 或 localhost_access.log

中没有看到任何 Jersey 日志

这就是我在 Eclipse 中使它在 Tomcat 运行 上工作的方式:

  1. 找出 Eclipse 中 tomcat 运行 的路径:Where can I view Tomcat log files in Eclipse?

  2. 在那里创建一个 logging.properties 文件。在我的例子中,路径是 .../EclipseWorkSpace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf (您可以从实际的 tomcat conf 文件夹中复制此日志文件。)

  3. 在您的应用程序中,在 src/main/resources 中创建一个 logging.properties 文件。 (当我直接在 WEB-INF/classes 中创建时不起作用,如 tomcat 文档中所述:

Example logging.properties for the servlet-examples web application to be placed in WEB-INF/classes inside the web application: from https://tomcat.apache.org/tomcat-9.0-doc/logging.html )

  1. 在上述文件中设置您的 tomcat 日志记录参数,它会覆盖应用程序内部 /.metadata/.plugins/org.eclipse.wst.server.core/tmp0/conf 的默认 logging.properties 文件:

    handlers= java.util.logging.ConsoleHandler
    
    .level= FINE
    
    java.util.logging.ConsoleHandler.level = FINE
    java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
    

这将记录来自 Tomcat 的 'FINE' 级日志。

  1. 在您的球衣应用程序中设置日志记录属性:

    register(new LoggingFeature(Logger.getLogger(MyApplication.class.getName()), Level.INFO, LoggingFeature.Verbosity.PAYLOAD_TEXT, 2048));
    

<init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.logging.LoggingFeature</param-value>
    </init-param>

这只会记录来自球衣的 'INFO' 日志。

因此,4 和 5 实际上意味着 Tomcat 会记录所有 FINE 日志,但 Jersey 只会记录 INFO 日志,因此在您的日志文件 (catalina.out) 中,您只会看到 INFO来自 Jersey 的日志,但 FINE 来自 Tomcat.

中其他所有内容的日志