如何让 JAXRS 2 (Jersey) verbose Trace 与 ResourceConfig 一起工作

How to get JAXRS 2 (Jersey) verbose Trace to work with ResourceConfig

我希望像这样输出 JAXRS 的调试日志(根据 documentation)...

  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

后者很有用,但没有显示事件链,即 START、PRE-MATCH、MATCH、REQ-FILTER、RI、INVOKE、RESP-FILTER、WI , 完成的。

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

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

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 来发现 restful 端点。

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

我应该指出,我正在使用具有此配置的 Log4J,我开始怀疑我是否需要用 Java 记录器/桥来做一些事情?

<?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>

我也试过将 Root 级别更改为 'trace'。

所以您尝试使用 LoggingFilter 来查看响应 headers,但看起来 headers 到LoggingFilter 被调用。但是 headers do 出现在实际响应 headers 中。您需要与您的客户核实才能看到它们。

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

org.glassfish.jersey.tracing.level=FINER

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