Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print server responses using LoggingFeature in Dropwizard 1.0.2?

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
like image 345
l0b0 Avatar asked Dec 03 '22 23:12

l0b0


2 Answers

A short example to illustrate a common issue which makes developers think that the logging feature is not working.

private static final LOG = Logger.getLogger(getClass().getName());

public void test() {
    Client client = ClientBuilder.newBuilder()
            .register(new LoggingFeature(LOG, Level.FINE, LoggingFeature.Verbosity.PAYLOAD_ALL, 8192))
            .build();
    // all requests and responses using this client will now be logged
    // with the log-level FINE to the logger LOG, but the logger
    // will simply ignore them, because it's default level is INFO
}

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:

new LoggingFeature(LOG, Level.INFO, LoggingFeature.Verbosity.PAYLOAD_ALL, 8192)

Solution 2: Decreasing the level of the Logger:

LOG.setLevel(Level.FINE)
like image 36
tobain Avatar answered Dec 15 '22 00:12

tobain


This does the trick:

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.

like image 90
l0b0 Avatar answered Dec 15 '22 00:12

l0b0