Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2 Filter particular level in apender

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

java:

LOGGER.debug("Debug message");
LOGGER.info("Info message");
LOGGER.warn("Warn message");
LOGGER.error("Error message");
LOGGER.fatal("Fatal message");

log4j2.xml:

<Configuration>
    <Appenders>
        <Console name="info-stdout-message">
            <PatternLayout pattern="[%logger{36}] %message %n" />
            <ThresholdFilter level="info"/>
        </Console>

        <Console name="detailed-stdout-message">
            <PatternLayout pattern="[%logger{36}] [%level] %message %n" />
        </Console>

        <File name="file-appender" fileName="logs/debug.log">
            <PatternLayout pattern="%d{HH:mm:ss dd.mm} [%t] [%-5level] %logger{36} - %msg %n" />
        </File>
    </Appenders>

    <Loggers>
        <Root level="debug">
            <AppenderRef ref="file-appender" level="debug" />
            <AppenderRef ref="info-stdout-message" level="info"/>
            <AppenderRef ref="detailed-stdout-message" level="info"/>
        </Root>
    </Loggers>
</Configuration>

The file output is fine, but in console I have such result:

[application.Main] [INFO] Info message
[application.Main] Info message
[application.Main] [WARN] Warn message
[application.Main] Warn message
[application.Main] [ERROR] Error message
[application.Main] Error message
[application.Main] [FATAL] Fatal message
[application.Main] Fatal message

but I need info-stdout-message appender to output only INFO messages, while detailed-stdout-message to output all EXEPT INFO. So the console output should looks like:

[application.Main] Info message
[application.Main] [WARN] Warn message
[application.Main] [ERROR] Error message
[application.Main] [FATAL] Fatal message

Can't find out how to prevent filters respect level inheritance. Is it possible to do this?

like image 849
ovnia Avatar asked Jul 11 '14 10:07

ovnia


4 Answers

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>
like image 89
Heri Avatar answered Nov 19 '22 21:11

Heri


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.

like image 42
downdrown Avatar answered Nov 19 '22 23:11

downdrown


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>
like image 17
Remko Popma Avatar answered Nov 19 '22 21:11

Remko Popma


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>
...
like image 1
myset Avatar answered Nov 19 '22 21:11

myset