log4j2 - log4j2 DynamicThresholdFilter 没有拾取属性值
问题描述
尝试为控制台和文件设置不同的打印级别。
目标是:当debug=true时,打印调试级别(控制台和文件),否则只打印错误级别(控制台)。
像这样以编程方式更改它:
@Override
public void contextInitialized(ServletContextEvent sce) {
ThreadContext.put("debugMode", "true");
}
这是我的 log4j2 配置:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Properties>
<Property name="logPath">${sys:catalina.home}</Property>
<Property name="rollingFileName">vsTenant</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<filters>>
<DynamicThresholdFilter key="debugMode" defaultThreshold="ERROR" onMatch="ACCEPT" onMismatch="NEUTRAL">
<KeyValuePair key="true" value="DEBUG"/>
<KeyValuePair key="false" value="ERROR"/>
</DynamicThresholdFilter>
</filters>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%thread] %-5level [vsTenant] %logger{36} - %msg\n%n" />
</Console>
<RollingFile name="rollingFile" fileName="${logPath}/logs/vsTenant.log" filePattern="${logPath}/logs/vsTenant_%d{dd-MM-yyyy}.log">
<filters>>
<DynamicThresholdFilter key="debugMode" onMatch="ACCEPT" onMismatch="DENY">
<KeyValuePair key="true" value="DEBUG"/>
</DynamicThresholdFilter>
</filters>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%thread] %-5level [vsTenant] %logger{36} - %msg\n%n" />
<Policies>
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="ERROR" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="rollingFile" />
</Root>
</Loggers>
</Configuration>
它总是打印错误级别。帮助表示赞赏。
解决方案
您已经很接近了,只需要进行一些小的更改即可实现您想要的。
onMismatch
将第一个从更改NEUTRAL
为DENY
KeyValuePair
在第二个过滤器中添加第二个:<KeyValuePair key="false" value="OFF"/>
Root
将日志级别从更改ERROR
为TRACE
这是修改后的配置:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="logPath">${sys:catalina.home}</Property>
<Property name="rollingFileName">vsTenant</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<filters>
<DynamicThresholdFilter key="debugMode" defaultThreshold="ERROR" onMatch="ACCEPT" onMismatch="DENY">
<KeyValuePair key="true" value="DEBUG"/>
<KeyValuePair key="false" value="ERROR"/>
</DynamicThresholdFilter>
</filters>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%thread] %-5level [vsTenant] %logger{36} - %msg\n%n" />
</Console>
<RollingFile name="rollingFile" fileName="${logPath}/logs/vsTenant.log" filePattern="${logPath}/logs/vsTenant_%d{dd-MM-yyyy}.log">
<filters>
<DynamicThresholdFilter key="debugMode" onMatch="ACCEPT" onMismatch="DENY">
<KeyValuePair key="true" value="DEBUG"/>
<KeyValuePair key="false" value="OFF"/>
</DynamicThresholdFilter>
</filters>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%thread] %-5level [vsTenant] %logger{36} - %msg\n%n" />
<Policies>
<SizeBasedTriggeringPolicy size="50 MB" />
</Policies>
<DefaultRolloverStrategy max="5"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="TRACE" additivity="false">
<AppenderRef ref="console" />
<AppenderRef ref="rollingFile" />
</Root>
</Loggers>
</Configuration>
这是一些用于测试它的java代码:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
public class SomeClass {
private static final Logger log = LogManager.getLogger();
public static void main(String[] args){
ThreadContext.put("debugMode", "false");
log.info("Info should not show anywhere");
log.debug("This shouldn't show anywhere");
ThreadContext.put("debugMode", "true");
log.debug("This should show in the log and console");
log.info("This should also show in both");
ThreadContext.put("debugMode", "false");
log.info("This should not show anywhere");
log.error("This error should show only in console.");
}
}
运行上述代码将向控制台输出以下内容:
21:16:40.716 [main] DEBUG [vsTenant] example.SomeClass - This should show in the log and console
21:16:40.718 [main] INFO [vsTenant] example.SomeClass - This should also show in both
21:16:40.718 [main] ERROR [vsTenant] example.SomeClass - This error should show only in console.
并将以下内容输出到日志文件:
21:16:40.716 [main] DEBUG [vsTenant] example.SomeClass - This should show in the log and console
21:16:40.718 [main] INFO [vsTenant] example.SomeClass - This should also show in both
推荐阅读
- vert.x - Vertx args 命令行文档
- swift - 使用 SwiftUI,我如何让一个 Toggle 改变另一个 Toggle 的状态?
- vue.js - 如何将传递给 axios 参数的数组格式化为以下特定格式:[0].mfr=mfr0&[0].mpn=mpn0&[1].mfr=mfr1&[1].mpn=mpn1
- c# - 无法使用 UWP 在工具栏项中显示按钮的图标
- ios - UITableViewCell 中的 UILabel 返回 nil
- android - 如何在andorid中更改firebase用户
- reactjs - 谷歌在反应中渲染谷歌地图时未定义
- c# - 如何使用 HotspotAuthentication 类组
- c# - 问题让我的数据模型进入 WPF 布局
- arrays - 在 Python 中不使用 for 循环返回数组/列表中的指定元素