How to log JSON responses in Dropwizard (Jersey)

I would like to know how to configure Dropwizard to register JSON response.

+8
java jersey dropwizard
source share
5 answers

In dropwizard 0.8.1 (also 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 the logger, add to your configuration file (for example: conf.yml ):

 logging: loggers: org.glassfish.jersey.filter.LoggingFilter: INFO 
+10
source share

In the Service subclass (ex HelloWorldService) in the startup 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 com.sun.jersey.api.container.filter.LoggingFilter (or any parent package) is configured at least at the INFO log level, for example:

 logging: loggers: "com.sun.jersey.api.container.filter.LoggingFilter": INFO 
+13
source share

Dropwizard 0.7.0 has the correct syntax for enabling request and response logging:

 environment.jersey().property(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, LoggingFilter.class.getName()); environment.jersey().property(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, LoggingFilter.class.getName()); 
+5
source share

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

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

Where logger is java.util.logging.Logger

+5
source share

The registration filter is out of date , so we must use LoggingFeature .

Unfortunately, I could not get it to work with @Click Upvote answer

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

The following code worked for me. They correspond to different designers.

  environment.jersey().register(new LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), Level.INFO, LoggingFeature.Verbosity.PAYLOAD_ANY, LoggingFeature.DEFAULT_MAX_ENTITY_SIZE)); 

Here are the constructors in both cases.

 public LoggingFeature(Logger logger, Integer maxEntitySize) { this(logger, (Level)null, DEFAULT_VERBOSITY, maxEntitySize); } public LoggingFeature(Logger logger, Level level, LoggingFeature.Verbosity verbosity, Integer maxEntitySize) { this.filterLogger = logger; this.level = level; this.verbosity = verbosity; this.maxEntitySize = maxEntitySize; } 

Level adjustment does the trick.

0
source share

All Articles