Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropwizard logging: add new appender for a particular logger

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!

like image 511
nightcrawler Avatar asked Jun 25 '15 20:06

nightcrawler


2 Answers

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 -

  • Dropwizard Logging Configuration
  • Dropwizard 0.9.0 Release Notes
like image 53
Denny Abraham Cheriyan Avatar answered Nov 13 '22 05:11

Denny Abraham Cheriyan


If you are wanting your custom logger to be the only logs in the log file, as well as going into the default appenders, you could create your own appender based on the Dropwizard logging spec.

Requirements:

Custom Appender factory

that extends io.dropwizard.logging.AbstractAppenderFactory (referred to as MyCustomFactory)

Custom configurations can be injected here from your .yml file by creating JsonProperties here, using Hibernate Annotations for any validations you need on the configuration file:

@NotNull
@JsonProperty
private String url;
@JsonProperty
private int maxBufferSize = 100;
@JsonProperty
private int sendDelayInSeconds = 10;

Give the class a @JsonTypeName("YourAppenderName"). This is how you reference the appender in your configuration.

This class needs access to any configuration you want to pass to your appender, since the function of this class is to create your appenders that Dropwizard will use.

Custom Appender

that extends ch.qos.logback.core.AppenderBase(Referred to as MyCustomAppender). You can write your own, or use an existing from from services like Loggly.

Inside the appender, check where the log is coming from and filter what you want to write to a file.

To tell Dropwizard about your new custom factory...

You need a file called io.dropwizard.logging.AppenderFactory placed into your src/main/resources/META-INF/services/ directory.

The contents of this file is the full name (include packages) for your custom Factory. (Example: com.myCompany.appender.MyCustomFactory)

Using the custom appender

In your yml file add the new appender by the name you specified:

appenders:
# Log warnings and errors to stderr
- type: console
  threshold: INFO
  target: stderr
# Custom Logger
- type: YourAppenderName
  threshold: INFO
  url: https://sendYourLogsHere/logs
like image 43
cbradsh1 Avatar answered Nov 13 '22 04:11

cbradsh1