首页 > 解决方案 > 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>

它总是打印错误级别。帮助表示赞赏。

标签: log4j2

解决方案


您已经很接近了,只需要进行一些小的更改即可实现您想要的。

  1. onMismatch将第一个从更改NEUTRALDENY
  2. KeyValuePair在第二个过滤器中添加第二个:<KeyValuePair key="false" value="OFF"/>
  3. Root将日志级别从更改ERRORTRACE

这是修改后的配置:

<?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

推荐阅读