问题描述
以下代码导致在Dropwizard 0.9.2和1.0.2中打印JSON服务器响应:
The following code results in JSON server responses being printed in Dropwizard 0.9.2 and 1.0.2:
return ClientBuilder
.newBuilder()
.build()
.register(new LoggingFilter(Logger.getLogger(LoggingFilter.class.getName()), true))
例如:
Oct 21, 2016 7:57:42 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Client response received on thread main
1 < 401
1 < Connection: keep-alive
1 < Content-Length: 49
1 < Content-Type: text/plain
1 < Date: Fri, 21 Oct 2016 07:57:42 GMT
1 < Server: […]
1 < WWW-Authenticate: Basic realm="[…]"
Credentials are required to access this resource.
javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
但是,在1.0.2中不推荐使用LoggingFilter
,建议改用LoggingFeature
.在 LoggingFeature文档中说默认的详细程度是LoggingFeature.Verbosity.PAYLOAD_TEXT
,所以我期望以下代码仍能在Dropwizard 1.0.2中打印JSON服务器响应:
However, LoggingFilter
is deprecated in 1.0.2, and it's recommended to use LoggingFeature
instead. In the documentation of LoggingFeature it says that the default verbosity is LoggingFeature.Verbosity.PAYLOAD_TEXT
, so I was expecting the following code to still print JSON server responses in Dropwizard 1.0.2:
return ClientBuilder
.newBuilder()
.build()
.register(new LoggingFeature(Logger.getLogger(getClass().getName())))
相反,日志仅包含以下内容:
Instead the log contains just this:
javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
推荐答案
这可以解决问题:
new LoggingFeature(Logger.getLogger(getClass().getName()), Level.OFF, LoggingFeature.Verbosity.PAYLOAD_TEXT, 8192)
我猜想客户端中的日志记录功能就像一个过滤器,而不是预期的包含功能.
I'm guessing the logging feature in the client acts like a filter, rather than as an include as expected.
这篇关于如何在Dropwizard 1.0.2中使用LoggingFeature打印服务器响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!