我有一个包含这些包的 spring 项目:
大部分逻辑都在核心包中,其他三个包都使用它。其他三个包不使用彼此的代码。
我需要 2 个日志文件:
问题 我可以在logback调试日志中看到以下信息:
[10/23/23 16:38:53:064 UTC] 0000008e SystemOut O 16:38:53,064 |-WARN in ch.qos.logback.core.model.processor.ImplicitModelHandler - Ignoring unknown property [turboFilter] in [ch.qos.logback.core.rolling.RollingFileAppender]
我可以在日志中看到MDC值,但过滤不起作用,两个日志文件都有所有日志条目。
我的日志文件:
<!-- LOCAL -->
<configuration>
<property name="APP.NAME" scope="context" value="App"/>
<property name="APP.LOG.PATH" scope="context" value="logs/APP"/>
<appender name="FILE.APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APP.LOG.PATH}/${APP.NAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APP.LOG.PATH}/${APP.NAME}%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10</maxHistory>
</rollingPolicy>
<turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
<MDCKey>specialLogger</MDCKey>
<Value>QUARTZ</Value>
<OnMatch>DENY</OnMatch>
<OnMismatch>ACCEPT</OnMismatch>
</turboFilter>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{dd.MM.YYYY HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%X{correlationId}] [%X{specialLogger}] - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE.APPENDER.QUARTZ" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APP.LOG.PATH}/${APP.NAME}_QUARTZ.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APP.LOG.PATH}/${APP.NAME}_QUARTZ%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10</maxHistory>
</rollingPolicy>
<turboFilter class="ch.qos.logback.classic.turbo.MDCFilter">
<MDCKey>specialLogger</MDCKey>
<Value>QUARTZ</Value>
<OnMatch>ACCEPT</OnMatch>
<OnMismatch>DENY</OnMismatch>
</turboFilter>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{dd.MM.YYYY HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%X{correlationId}] [%X{specialLogger}] - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE.APPENDER" />
<appender-ref ref="FILE.APPENDER.QUARTZ" />
</root>
</configuration>
依赖关系:
编辑: 我还尝试仅记录到“FILE.APPENDER.QUARTZ”,它应该只记录基于预定作业过滤器的条目。无论如何,它包含所有日志条目。
我找到了解决方案。 无法使用turboFilters 来实现此功能。 相反,我必须使用自定义标准过滤器。
我的日志文件:
<!-- LOCAL -->
<configuration debug="true">
<property name="APP.NAME" scope="context" value="PdfGen"/>
<property name="APP.LOG.PATH" scope="context" value="logs/APP"/>
<appender name="FILE.APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APP.LOG.PATH}/${APP.NAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APP.LOG.PATH}/${APP.NAME}%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10</maxHistory>
</rollingPolicy>
<filter class="<package_path>.MDCFilterRegular">
<MDCKey>specialLogger</MDCKey>
<Value>QUARTZ</Value>
<OnMatch>DENY</OnMatch>
<OnMismatch>ACCEPT</OnMismatch>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{dd.MM.YYYY HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%X{uniqueNumber}] [%X{correlationId}] - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE.APPENDER.QUARTZ" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${APP.LOG.PATH}/${APP.NAME}_QUARTZ.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${APP.LOG.PATH}/${APP.NAME}_QUARTZ%d{yyyy-MM-dd}.%i.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>10</maxHistory>
</rollingPolicy>
<filter class="<package_path>.MDCFilterRegular">
<MDCKey>specialLogger</MDCKey>
<Value>QUARTZ</Value>
<OnMatch>ACCEPT</OnMatch>
<OnMismatch>DENY</OnMismatch>
</filter>
<encoder>
<charset>UTF-8</charset>
<pattern>%d{dd.MM.YYYY HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%X{uniqueNumber}] [%X{correlationId}] - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<charset>UTF-8</charset>
<pattern>%d{dd.MM.YYYY HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root>
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE.APPENDER.QUARTZ" />
<appender-ref ref="FILE.APPENDER" />
</root>
</configuration>
自定义过滤器:
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.filter.AbstractMatcherFilter;
import ch.qos.logback.core.spi.FilterReply;
public class MDCFilterRegular extends AbstractMatcherFilter<ILoggingEvent> {
String MDCKey;
String Value;
public MDCFilterRegular() {
}
public void start() {
if (this.MDCKey != null && this.Value != null) {
super.start();
}
}
@Override
public FilterReply decide(ILoggingEvent event) {
if(!event.getMDCPropertyMap().containsKey(this.MDCKey)) {
return this.onMismatch;
}
return event.getMDCPropertyMap().get(this.MDCKey).equals(Value) ? this.onMatch : this.onMismatch;
}
public String getMDCKey() {
return MDCKey;
}
public void setMDCKey(String MDCKey) {
this.MDCKey = MDCKey;
}
public String getValue() {
return Value;
}
public void setValue(String value) {
this.Value = value;
}
}