log4j2 DynamicThresholdFilter未获取属性值

时间:2019-01-22 08:43:23

标签: log4j2

尝试为控制台和文件设置不同的打印级别。

目标是:当 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>

它始终在打印错误级别。感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您很亲近,只需少量更改即可实现您想要的。

  1. 将第一个onMismatchNEUTRAL更改为DENY
  2. 将第二个KeyValuePair添加到第二个过滤器:<KeyValuePair key="false" value="OFF"/>
  3. 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