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))
For example:
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
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
so - I tested this. This may be an issue concerning your logger configuration and your feature configuration, not the actual feature. The hint I gave you does print what you want it to, and here is the proof:
This prints:
So, here are the pitfalls:
Your feature MUST be configured with the right level. See:
The logger must be configured to log the right stuff.
And the feature must have the right verbosity:
Verbosity.PAYLOAD_ANY
And that is all you need to do.
Regards,
Artur
This does the trick:
I'm guessing the logging feature in the client acts like a filter, rather than as an include as expected.
A short example to illustrate a common issue which makes developers think that the logging feature is not working.
The created logger instance
LOG
uses the default log level, which is INFO. That means it will accept all log messages with an level of at least INFO or higher (WARNING, SEVERE, ...), but it will ignore all messages with a lower level, like FINE. (It will still pass the message to it's parent logger, if there is one)Notice: The default log level for handlers is Level.ALL and they should not reject any log records, as long you don't modify their level.
So you will need either to raise the LoggingFeatures level or lower the loggers level to see the messages.
Solution 1: Increasing the level of the LoggingFeature:
Solution 2: Decreasing the level of the Logger:
For logging the custom logs for Server and Client request/response using the LoggingFeature, you need to create a custom class that extends LoggingFeature and implement ContainerRequestFilter, ContainerResponseFilter, ClientRequestFilter, ClientResponseFilter and WriterInterceptor.
You need to override the LoggingFeature method
public boolean configure(FeatureContext context)
and register your CustomLoggingFeature object with the contextContainerRequestFilter and ContainerResponseFilter interface has methods used to log the Server request and response.
ClientRequestFilter and ContainerResponseFilter interface has methods used to log the Client request and response.
Also, you need to implement the
WriterInterceptor
to log the Client request and Server response body.Finally registered your CustomLoggingFeature class with jersey.
I am using Dropwizard v1.3.5.
Here is my implementation.
For me, the solution by @l0b0 did log request/response payloads, but my access logs logged the requests and responses as errors. The following line worked correctly for me: