Log4j2 Filter particular level in apender

前端 未结 5 752
天命终不由人
天命终不由人 2020-12-09 12:12

What filter should i use, to define particular level to be logged with apender? For example:

java:

LOGGER.debug(\"Debug message\");
LOGGER.info(\"Inf         


        
相关标签:
5条回答
  • 2020-12-09 12:46

    Actually, from your question it looks like you don't want two different appenders, but two different patterns that are using under different circumstances. For that you should just use a PatternSelector.

        <Console name="stdout-message">
          <ScriptPatternSelector defaultPattern="[%logger{36}] [%level] %message %n">
            <Script name="BeanShellSelector" language="bsh"><![CDATA[
              if (logEvent.getLevel() == Level.INFO) {
                return "INFO";
              } else {
                return null;
              }]]>
            </Script>
            <PatternMatch key="INFO" pattern="[%logger{36}] %message %n"/>
           </ScriptPatternSelector>
        </Console>
    
    0 讨论(0)
  • 2020-12-09 12:48

    Here two console appenders. One logs all trace, debug and info levels to std_out, the other logs all warn, error and fatal levels to std_err. This is very useful e.g. within eclipse since std_err is displayed red.

    <Console name="STDOUT" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} (%6r) %-5p [%-7t] %F:%L %x - %m%n" />
        <Filters>
            <ThresholdFilter level="warn" onMatch="DENY" onMismatch="ACCEPT" />
        </Filters>
    </Console>
    
    <Console name="STDERR" target="SYSTEM_ERR">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} (%6r) %-5p [%-7t] %F:%L %x - %m%n" />
        <Filters>
            <ThresholdFilter level="WARN" onMatch="ACCEPT" />
        </Filters>
    </Console>
    
    0 讨论(0)
  • 2020-12-09 12:58

    It took me very long to figure out how to filter a range of LogLevels via the log4j2.xml configuration file. In the End this worked for me :

    <Filters>
        <ThresholdFilter level="trace" />
        <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL" />
    </Filters>
    

    In this case Log4J will log all messages from Level.TRACE to Level.DEBUG, all levels below Level.DEBUG will be ignored.

    0 讨论(0)
  • 2020-12-09 13:00

    This works:

    <Console name="info-stdout-message">
        <PatternLayout pattern="[%logger{36}] %message %n" />
        <Filters>
    
            <!-- Now deny warn, error and fatal messages -->
            <ThresholdFilter level="warn"  onMatch="DENY"   onMismatch="NEUTRAL"/>
    
            <!-- This filter accepts info, warn, error, fatal and denies debug/trace -->
            <ThresholdFilter level="info"  onMatch="ACCEPT" onMismatch="DENY"/>
        </Filters>
    </Console>
    
    0 讨论(0)
  • 2020-12-09 13:07

    Please check all log4j2 filtering possibilities at https://logging.apache.org/log4j/2.0/manual/filters.html

    Sample configuration fragment

    ...    
    <Console name="DEFAULT" target="SYSTEM_OUT">
        <PatternLayout>
            <Pattern>[%d][%p][%c:%L:%M] - %m%n</Pattern>
        </PatternLayout>
        <Filters>
            <RegexFilter regex="(?s).*(sql01|sql02|sql03).*" onMatch="DENY" onMismatch="NEUTRAL"/>
        </Filters>
    </Console>
    ...
    
    0 讨论(0)
提交回复
热议问题