以下代码导致在Dropwizard 0.9.2和1.0.2中打印JSON服务器响应:
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
。在documentation of LoggingFeature中,它说默认的详细程度是LoggingFeature.Verbosity.PAYLOAD_TEXT
,因此我期望以下代码仍在Dropwizard 1.0.2中打印JSON服务器响应:return ClientBuilder
.newBuilder()
.build()
.register(new LoggingFeature(Logger.getLogger(getClass().getName())))
相反,日志仅包含以下内容:
javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
最佳答案
这是诀窍:
new LoggingFeature(Logger.getLogger(getClass().getName()), Level.OFF, LoggingFeature.Verbosity.PAYLOAD_TEXT, 8192)
我猜客户端中的日志记录功能就像一个 过滤器 ,而不是像预期的那样包含。
关于java - 如何在 Dropwizard 1.0.2 中使用 LoggingFeature 打印服务器响应?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40171273/