【问题标题】:Jersey 2.23 : Not able to enable LoggingFeature from web.xmlJersey 2.23:无法从 web.xml 启用 LoggingFeature
【发布时间】:2017-12-20 22:00:48
【问题描述】:

我正在尝试启用在 Tomcat 上运行的 Jersey 中的 HTTP 请求/响应日志记录。我的应用程序基于 web.xml,并且在 ResourceConfig 类上没有应用程序。

这就是我尝试从我的 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 日志

【问题讨论】:

    标签: java jersey-2.0


    【解决方案1】:

    这就是我如何让它在 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 文档中所述:

    servlet-examples Web 应用程序的示例 logging.properties 放在 web 应用程序内的 WEB-INF/classes 中: 来自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) 中,您只会看到来自 Jersey 的 INFO 日志,但来自 Jersey 的 FINE 日志Tomcat 中的所有其他内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 2017-05-24
      • 1970-01-01
      • 2013-06-23
      • 2014-06-20
      相关资源
      最近更新 更多