Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log JSON responses in Dropwizard (Jersey)

I would like to know how one would configure Dropwizard to log the JSON response.

like image 928
Dale Wijnand Avatar asked Jun 20 '13 12:06

Dale Wijnand


3 Answers

In the Service subclass (ex HelloWorldService), in the run method, add:

environment.setJerseyProperty(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, LoggingFilter.class.getName());
environment.setJerseyProperty(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, LoggingFilter.class.getName());

and then make sure that com.sun.jersey.api.container.filter.LoggingFilter (or any parent package) is configured at least at log level INFO, for example:

logging:
  loggers:
    "com.sun.jersey.api.container.filter.LoggingFilter": INFO
like image 173
Dale Wijnand Avatar answered Nov 03 '22 00:11

Dale Wijnand


In dropwizard 0.8.1 (also tried in 0.9.0-SNAPSHOT), add to Application.run(...):

import java.util.logging.Logger;
import org.glassfish.jersey.filter.LoggingFilter;
...
public void run(MyApplicationConfiguration conf, Environment env) throws Exception {
    // do your stuff and then add LoggingFilter
    env.jersey().register(new LoggingFilter(
                     Logger.getLogger(LoggingFilter.class.getName()),
                     true)
                 );
}

To configure logger, add in your configuration file (e.g.:conf.yml):

logging:
  loggers:
    org.glassfish.jersey.filter.LoggingFilter: INFO
like image 10
joao cenoura Avatar answered Nov 03 '22 02:11

joao cenoura


The answers are a bit outdated, this is how it needs to be done in newer versions:

env.jersey().register(new LoggingFeature(logger, LoggingFeature.Verbosity.PAYLOAD_ANY));

Where logger is a java.util.logging.Logger

like image 9
Ali Avatar answered Nov 03 '22 02:11

Ali