Jersey客户端2.25记录不适用于请求/响应流量

时间:2017-02-25 10:39:48

标签: java rest logging jersey-client

我正在使用针对REST自动化框架的jersey客户端v2.25.1,以下是我如何注册Logger并初始化客户端:

//Initialize FileHandler
Path currPath = Paths.get(System.getProperty("user.dir"));
Path logFilePath = Paths.get(currPath.toString(), "logs", testCaseName+".log");
logHandleObj = new FileHandler(logFilePath.toString(), 0, 1, true);

//Initialize Logger
Logger loggerObj = Logger.getLogger(testCaseName);
loggerObj.setUseParentHandlers(false);
loggerObj.addHandler(logHandleObj);

//Initialize ClientConfig
ClientConfig clientConfig = new ClientConfig();
//Set configuration features
clientConfig.register(new JacksonFeature());
clientConfig.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
clientInstance = ClientBuilder.newClient(clientConfig);
//Register LoggingFeature
clientInstance.register(new LoggingFeature(loggerObj, Level.ALL, 
        LoggingFeature.Verbosity.PAYLOAD_ANY, LoggingFeature.DEFAULT_MAX_ENTITY_SIZE));

根据documentation,这应该记录完整的请求/响应流量,如下所示:

1 May 09, 2016 2:55:33 PM org.glassfish.jersey.logging.LoggingInterceptor log
2 INFO: 1 * Server has received a request on thread grizzly-http-server-0
3 1 > GET http://localhost:9998/helloworld
....

然而,我得到了:

<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
    <date>2017-02-25T15:30:06</date>
    <millis>1488016806253</millis>
    <sequence>0</sequence>
    <logger>org.glassfish.jersey.client.JerseyInvocation</logger>
    <level>WARNING</level>
    <class>org.glassfish.jersey.client.JerseyInvocation</class>
    <method>validateHttpMethodAndEntity</method>
    <thread>1</thread>
    <message>Entity must not be null for http method PUT.</message>
</record>

这里有两个问题:

  • 未获得HTTP流量
  • 由于某种原因,格式是XML而不是纯文本

非常感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:0)

因此,在到处寻找之后,我继续尝试了我能想到的一切,并最终找到了解决方案。

logHandleObj = new FileHandler(logFilePath.toString());
A-> SimpleFormatter formatter = new SimpleFormatter();

Logger loggerObj = Logger.getLogger(testCaseName);
A-> logHandleObj.setFormatter(formatter);
B-> loggerObj.setLevel(Level.FINEST);
loggerObj.setUseParentHandlers(false);

在上面添加标有A的行修复了XML格式的问题(这很容易),添加标记为B的行修复了日志文件中没有出现的HTTP流量问题。

总而言之,Level应明确设置为FINEST才能使其正常工作,这在文档中无处可寻。 希望这有助于某人。