除指定记录器外的所有记录器的记录器配置

时间:2020-10-14 15:25:16

标签: java logging log4j2

我有以下演示代码:

package playground;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Practice {
    private static Logger log = LogManager.getLogger();

    public static void main(String[] args) {
        log.debug("Hello, world!");
    }
}

我希望所有从包'playground'记录的日志都从'debug'级别向上记录到file1.log,并且所有其他记录器仅在出错或以上时才记录到file2.log。我尝试了以下配置:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info">
    <Appenders>
        <RollingFile name="File1"
                     fileName="file1.log"
                     filePattern="file1-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="1 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="15"/>
        </RollingFile>
        <RollingFile name="File2"
                     fileName="file2.log"
                     filePattern="file2-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy />
                <SizeBasedTriggeringPolicy size="1 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="15"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="playground" level="debug">
            <AppenderRef ref="File1"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="File2"/>
        </Root>
    </Loggers>
</Configuration>

但是,我发现自己的“世界你好!”日志消息同时发送到file1.log和file2.log。如何使我的“操场”记录器仅 进入file1?

0 个答案:

没有答案