9

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

Dale Wijnand
  • 6,054
  • 5
  • 28
  • 55

5 Answers5

13

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
Dale Wijnand
  • 6,054
  • 5
  • 28
  • 55
  • For some reason the logger doesn't seem to configure the level? – Sarp Kaya Nov 06 '14 at 01:31
  • Note that this was in the Dropwizard 0.6.x days. In 0.7.0 it might have changed. I never built anything with Dropwizard 0.7.0, and have moved away from it, so I'm not sure. Sorry. – Dale Wijnand Nov 06 '14 at 07:24
  • LoggingFilter internally calls logger.info(...) Here is the snippet: private void log(final StringBuilder b) { if (logger != null) { logger.info(b.toString()); } } – Ihor M. Sep 27 '16 at 17:30
10

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
joao cenoura
  • 1,155
  • 2
  • 14
  • 20
9

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

Ali
  • 261,656
  • 265
  • 575
  • 769
6

In dropwizard 0.7.0 the correct syntax to enable request and response logging is:

environment.jersey().property(ResourceConfig.PROPERTY_CONTAINER_REQUEST_FILTERS, LoggingFilter.class.getName());
environment.jersey().property(ResourceConfig.PROPERTY_CONTAINER_RESPONSE_FILTERS, LoggingFilter.class.getName());
elektromin
  • 79
  • 1
  • 4
3

Logging Filter is deprecated, so we should be using LoggingFeature.

Unfortunately I could not get it working with @Click Upvote's answer of

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

Following Code worked for me. They correspond to different constructors.

    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 the 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;
}

Setting the level is doing the trick.

Ramesh
  • 1,287
  • 1
  • 9
  • 14