我们正在使用带有DynamicThreshholdFilter定义的logback,如下所示
<turboFilter class="ch.qos.logback.classic.turbo.DynamicThresholdFilter">
<Key>USER_MDC_KEY</Key>
<DefaultThreshold>DEBUG</DefaultThreshold>
<MDCValueLevelPair>
<value>USER1</value>
<level>DEBUG</level>
</MDCValueLevelPair>
</turboFilter>
是否可以通过编程方式修改MDCValueLevelPair的设置。例如,将USER1的级别修改为INFO。
不能动态地[[修改,因为有检查阻止它。
但是,您可以通过以下方式动态设置它:Iterator<TurboFilter> it = ((ch.qos.logback.classic.LoggerContext)LoggerFactory.getILoggerFactory()).getTurboFilterList().iterator();
while (it.hasNext()) {
TurboFilter f = it.next();
if (f instanceof DynamicThresholdFilter) {
DynamicThresholdFilter dtFilter = (DynamicThresholdFilter)f;
MDCValueLevelPair pair = new MDCValueLevelPair();
pair.setValue("USER1");
pair.setLevel(Level.INFO);
dtFilter.addMDCValueLevelPair(pair);
}
}
如果尚未配置TurboFilter
,则可以创建一个新的并将其添加到LoggerContext
。
<configuration>
<turboFilter class="ch.qos.logback.classic.turbo.DynamicThresholdFilter">
<Key>LEVEL_FOR_USER1</Key>
<DefaultThreshold>DEBUG</DefaultThreshold>
<MDCValueLevelPair>
<value>INFO</value>
<level>INFO</level>
</MDCValueLevelPair>
</turboFilter>
......
</configuration>
MDC.put("LEVEL_FOR_USER1", "INFO");