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!
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 -
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.
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.
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With