Dropwizard logging: add new appender for a particular logger

nightcrawler picture nightcrawler · Jun 25, 2015 · Viewed 15.9k times · Source

I'm new to dropwizard and am trying to figure out ways to configure logging better.

I have registered a new logger in a bundle like so:

Logger log = LoggerFactory.getLogger("mylogger");
log.info("this is a log from mylogger");

Now I'm using this bundle in a bunch of services. By default any log that comes through this logger would be written to the application log file.

The problem I'm trying to solve is this: I want all logs written by mylogger(only) to go to a new file. It is fairly starightforward to add a new appender to the service yml file like:

logging:

  loggers:

  appenders:
  - type: file.
    currentLogFilename: ./logs/example.log
    archivedLogFilenamePattern: ./logs/example-%d.log.gz
    archivedFileCount: 5

But this would mean that all of application logs would now be written to example.log. I do not know of a way to specify a logger specifically for this appender which does not affect/alter already existing logging.

Can someone tell me if there's a way to do this in dropwizard? Thanks!

Answer

Denny Abraham Cheriyan picture Denny Abraham Cheriyan · Nov 3, 2015

In Dropwizard 0.9.0 (released on Oct 28th 2015), they have added support for individual logger appenders and disabling logger additivity.

To achieve what you have described, you can specify the following in your yaml configuration file -

logging:
  level: INFO
  loggers:
    "mylogger":
      level: DEBUG
      additive: false
      appenders:
        - type: file
          currentLogFilename: /var/log/mylogger.log
          archivedLogFilenamePattern: /var/log/mylogger-%d.log.gz
          archivedFileCount: 5
  appenders:
    - type: console

Setting additive to false will prevent the logger (or anything under it) from writing to appenders which are hierarchically above it, including the root logger.

References -