问题描述
问题类似于-How to configure log4j to log different log levels to different files for the same logger下所述的问题。我在这里可以找到的现有问题主要是关于logback和log4j配置的。在Dropwizard配置中专门寻找一些解决方案来解决此问题。
曾经尝试在Internet上四处浏览,似乎可以使用LevelMatchFilter
,但无法弄清楚Dropwizard的文档。
我目前拥有的是
logging:
level: INFO
appenders:
- type: file
threshold: INFO
logFormat: '%date{dd-MM-yyyy HH:mm:ss.SSS} %X{X-Request-Id} [%thread] %-5level %logger{36} - %msg%n'
currentLogFilename: .../info.log
archivedLogFilenamePattern: .../info-%i.log.gz
archivedFileCount: 2
timeZone: IST
maxFileSize: 100MB
- type: file
threshold: WARN
currentLogFilename: .../warn.log
archivedLogFilenamePattern: .../warn-%i.log.gz
... other attributes
- type: file
threshold: ERROR
...similar attributes for error logs
但这会导致log.error
是三个文件的一部分,而我打算执行的操作是确保它只是error.log
文件的一部分并与其他文件相关。
解决方法
Dropwizard具有自己的日志记录配置,并且支持adding filters by writing FilterFactory classes。
例如,这是一个FilterFactory,它过滤掉除错误级别日志事件以外的所有内容:
@JsonTypeName("error-only")
public class ErrorLogFilter implements FilterFactory<ILoggingEvent> {
@Override
public Filter<ILoggingEvent> build() {
return new Filter<ILoggingEvent>() {
@Override
public FilterReply decide(ILoggingEvent event) {
if (event.getLevel().equals(Level.ERROR)) {
return FilterReply.NEUTRAL;
} else {
return FilterReply.DENY;
}
}
};
}
}
要注册工厂,必须在META-INF / services / io.dropwizard.logging.filter.FilterFactory文件中列出其完全合格的类名称。
获取要使用其中一个文件追加程序的配置,如下所示:
- type: file
threshold: ERROR
logFormat: '%date{dd-MM-yyyy HH:mm:ss.SSS} %X{X-Request-Id} [%thread] %-5level %logger{36} - %msg%n'
currentLogFilename: .../error.log
archivedLogFilenamePattern: .../error-%i.log.gz
archivedFileCount: 2
timeZone: IST
maxFileSize: 100MB
filterFactories:
- type: error-only