如何让 JAXRS 2 (Jersey) 详细 Trace 与 ResourceConfig 配合使用

2024-04-05

我希望像这样输出 JAXRS 的调试日志记录(根据文档 https://jersey.java.net/documentation/latest/monitoring_tracing.html#d0e16356)...

  3 X-Jersey-Tracing-000: START       [ ---- /  ---- ms |  ---- %] baseUri=[http://localhost:9998/ALL/] requestUri=[http://localhost:9998/ALL/root/sub-resource-locator/sub-resource-method] method=[POST] authScheme=[n/a] accept=[application/x-jersey-test] accept-encoding=n/a accept-charset=n/a accept-language=n/a content-type=[application/x-jersey-test] content-length=[11]
  4 X-Jersey-Tracing-001: PRE-MATCH   [ 0.01 /  0.68 ms |  0.01 %] PreMatchRequest summary: 2 filters
  5 X-Jersey-Tracing-002: MATCH       [ 8.44 /  9.15 ms |  4.59 %] RequestMatching summary
  6 X-Jersey-Tracing-003: REQ-FILTER  [ 0.01 /  9.20 ms |  0.00 %] Request summary: 2 filters
  7 X-Jersey-Tracing-004: RI          [86.14 / 95.49 ms | 46.87 %] ReadFrom summary: 3 interceptors
  8 X-Jersey-Tracing-005: INVOKE      [ 0.04 / 95.70 ms |  0.02 %] Resource [org.glassfish.jersey.tests.integration.tracing.SubResource @901a4f3] method=[public org.glassfish.jersey.tests.integration.tracing.Message org.glassfish.jersey.tests.integration.tracing.SubResource.postSub(org.glassfish.jersey.tests.integration.tracing.Message)]
  9 X-Jersey-Tracing-006: RESP-FILTER [ 0.01 / 96.55 ms |  0.00 %] Response summary: 2 filters
 10 X-Jersey-Tracing-007: WI          [85.95 / 183.69 ms | 46.77 %] WriteTo summary: 4 interceptors
 11 X-Jersey-Tracing-008: FINISHED    [ ---- / 183.79 ms |  ---- %] Response status: 200/SUCCESSFUL|OK

但我所能实现的只是这个……

03-Feb-2016 13:18:17.027 INFO [http-nio-8084-exec-270] org.glassfish.jersey.filter.LoggingFilter.log 3 * Server has received a request on thread http-nio-8084-exec-270
3 > GET http://localhost:8084/SocialScheduler/rest/dostuff?startDate=03%2F02%2F2016%2013%3A17&endDate=04%2F02%2F2016%2013%3A17&resolutionMins=720&_=1454505462184
3 > accept: application/json, text/javascript, */*; q=0.01
3 > accept-encoding: gzip, deflate, sdch
3 > accept-language: en-US,en;q=0.8
3 > connection: keep-alive
3 > cookie: JSESSIONID=7BDAEC5068C7A8F6FE26263EBF6998CE; cookieconsent_dismissed=yes; cookie_assistant_enable_cookies=true; __uvt=; _ga=GA1.1.1324172961.1446064949; _gat=1; __smToken=6obzPmlxzCuYugw7AHhVSuH7; linkedin_oauth_773fp8xagqy7nf_crc=null; uvts=3jj9UU7vonmXljiC
3 > host: localhost:8084
3 > referer: http://localhost:8084/projectx/app.jsp
3 > user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36
3 > x-requested-with: XMLHttpRequest

03-Feb-2016 13:18:17.080 INFO [http-nio-8084-exec-270] org.glassfish.jersey.filter.LoggingFilter.log 3 * Server responded with a response on thread http-nio-8084-exec-270
3 < 200
3 < Content-Type: application/json

后者很有用,但它没有向我显示事件链,即开始、预匹配、匹配、REQ-FILTER、RI、INVOKE、RESP-FILTER、WI、FINISHED。

在 Amazon Web Services (AWS) 下,我的 AJAX 请求似乎在大约 12 小时后冻结(并不总是完全相同的时间),因此我希望看到的不仅仅是 HTTP 标头;我在链条中走了多远/它卡在哪里了?!

这是我目前拥有的代码......

package uk.co.devology.projectx;

import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("/rest/*")
public class RestResourceConfig extends ResourceConfig {

    public RestResourceConfig() {
        packages("uk.co.devology.projectx");

        register(new LoggingFilter());

//        property(ServerProperties.TRACING, "ALL");
//        property(ServerProperties.TRACING_THRESHOLD, "SUMMARY");
        property("jersey.config.server.tracing", "ALL");
        property("jersey.config.server.tracing.threshold", "VERBOSE");

    }

}

看起来所有的日志记录都是由线路执行的

register(new LoggingFilter());

并且以下几行似乎没有执行任何操作

property("jersey.config.server.tracing", "ALL");
property("jersey.config.server.tracing.threshold", "VERBOSE");

没有 web.xml 文件,它结合使用 Servlet 3 规范和 JAXRS 来发现静态端点。

在旧版本的 JAXRS 中,这似乎更简单。

我应该指出,我正在使用 Log4J 进行此配置,并且我开始怀疑是否需要使用 Java Logger/Bridge 做一些事情?

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%p %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="debug">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

我也尝试过将根级别更改为“跟踪”。


所以你想要使用的是LoggingFilter查看响应标头,但看起来标头尚未添加LoggingFilter叫做。标头do但显示在实际的响应标头中。您需要与您的客户核实才能看到它们。

就登录服务器而言,日志记录级别似乎需要设置为FINER查看所有日志记录。我测试过,情况似乎是这样。所有日志都显示为FINER。仅将跟踪日志记录级别设置为FINER,您可以将其添加到您的日志记录属性文件中

org.glassfish.jersey.tracing.level=FINER

请注意,这是 Java Util Logging。如果您不确定如何使用和配置此日志记录框架,这里有很多教程。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何让 JAXRS 2 (Jersey) 详细 Trace 与 ResourceConfig 配合使用 的相关文章

随机推荐