Here's what you need to do:
- Implement or find some existing code for custom logback filter.
Here's an example of filter that passes only ERROR
-level messages:
package foo;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.Filter;
import ch.qos.logback.core.spi.FilterReply;
import com.fasterxml.jackson.annotation.JsonTypeName;
import io.dropwizard.logging.filter.FilterFactory;
@JsonTypeName("errors-only")
public class ErrorsOnlyFilterFactory implements FilterFactory<ILoggingEvent> {
@Override
public Filter<ILoggingEvent> build() {
return new Filter<>() {
@Override
public FilterReply decide(ILoggingEvent event) {
return event.getLevel() == Level.ERROR ?
FilterReply.ACCEPT :
FilterReply.DENY;
}
};
}
}
- Create file
<resources>/META-INF/services/io.dropwizard.logging.filter.FilterFactory
and populate it with filter class name. For my example, file contains only one line:
foo.ErrorsOnlyFilterFactory
- Add value of
@JsonTypeName
annotation to appender config, under filterFactories/type
(note: type
is an array, so you can specify multiple filters per appender if needed):
logging:
appenders:
- type: file
currentLogFilename: errors.log
...
filterFactories:
- type: errors-only
For every logging level that you want to log separately, you should:
- create new filter class
- add new line to
META-INF/services/io.dropwizard.logging.filter.FilterFactory
- add new appender config