简体   繁体   中英

Log4j2 Define 2 root loggers with different levels

I have 2 appenders I'm using - Console and custom appender called MyAppender which should ignore all messages with levels lower than ERROR (meaning - it should only support ERROR and FATAL). The Console should be able to support all levels. I've tried several ways to define it but it seems that the filter doesn't work for the root level appenders. How can this be achieved? My current log4j2 definition:

    <?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.mysample.logging.appenders" status="DEBUG">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <MyAppender name="MyAppender">
            <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
        </MyAppender>
    </Appenders>
    <Loggers>
        <Root level="ALL">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="MyAppender" />
        </Root>
    </Loggers>
</Configuration>

The loggers are one hierarchy starting with the root logger. There are no two root loggers within one working setup of Log4j.

What you want to achieve is to have different thresholds on the appenders. Check the link that Piotr suggested:

Using log4j2, is it possible to assign a specific level to a appender?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM