使用标准配置类在初始化后更改配置

时间:2018-08-06 04:50:23

标签: java log4j2

我正在尝试为以下情况创建参考示例。

Programmatically Modifying the Current Configuration after Initialization

  

推荐的用于自定义配置的方法是扩展标准配置类之一,覆盖设置方法以首先执行super.setup(),然后将自定义的Appenders,Filters和LoggerConfigs添加到配置中,然后再注册使用。

我到目前为止所拥有的。

Xml配置文件:log4j2_config_31_initialConfigFile.xml

<?xml version='1.0' encoding='UTF-8'?>
<Configuration>
    <Appenders>
        <Console name="appender1" target="SYSTEM_OUT">
            <PatternLayout charset="UTF-8" pattern="[appender1|%date|%logger|%level] message='%msg' exception='%exception'\n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="logger1" level="ALL" additivity="false">
            <AppenderRef ref="appender1"/>
        </Logger>
    </Loggers>
</Configuration>

扩展的标准配置类:personal.learn.loggers.log4j2.log4j2_all.modifying_currentConfiguration_after_initialization.CustomXmlConfiguration2_

package personal.learn.loggers.log4j2.log4j2_all.modifying_currentConfiguration_after_initialization;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.ConsoleAppender;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.config.xml.XmlConfiguration;
import org.apache.logging.log4j.core.layout.PatternLayout;

import java.nio.charset.StandardCharsets;

/*https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/config/xml/XmlConfiguration.html*/
public class CustomXmlConfiguration2_ extends XmlConfiguration {

    public CustomXmlConfiguration2_(LoggerContext loggerContext, ConfigurationSource configSource) {
        super(loggerContext, configSource);
    }

    @Override
    public void setup() {
        super.setup();

        final String appender2_name = "appender2";
        final String appender3_name = "appender3";
        final String logger2_name = "logger2";

        final PatternLayout patternLayout_for_appender2 = PatternLayout
                .newBuilder()
                .withCharset(StandardCharsets.UTF_8)
                .withPattern("[" + appender2_name + "|%date|%logger|%level] message='%msg' exception='%exception'\n")
                .build();
        final PatternLayout patternLayout_for_appender3 = PatternLayout
                .newBuilder()
                .withCharset(StandardCharsets.UTF_8)
                .withPattern("[" + appender3_name + "|%date|%logger|%level] message='%msg' exception='%exception'\n")
                .build();
        final ConsoleAppender consoleAppender_appender2 = ConsoleAppender
                .newBuilder()
                .withName(appender2_name)
                .setTarget(ConsoleAppender.Target.SYSTEM_OUT)
                .withLayout(patternLayout_for_appender2)
                .build();
        final ConsoleAppender consoleAppender_appender3 = ConsoleAppender
                .newBuilder()
                .withName(appender3_name)
                .setTarget(ConsoleAppender.Target.SYSTEM_OUT)
                .withLayout(patternLayout_for_appender3)
                .build();

        synchronized (this) {
            consoleAppender_appender2.start();
            consoleAppender_appender3.start();
            this.addAppender(consoleAppender_appender2);
            this.addAppender(consoleAppender_appender3);
        }

        final LoggerConfig loggerConfig_logger2 = new LoggerConfig(logger2_name, Level.ALL, false);
        loggerConfig_logger2.addAppender(consoleAppender_appender2, Level.ALL, null);

        synchronized (this) {
            final LoggerConfig obtained_logger1 = this.getLoggerConfig("logger1");
            obtained_logger1.addAppender(consoleAppender_appender3, Level.ALL, null);

            this.addLogger(logger2_name, loggerConfig_logger2);
            this.getLoggerContext().updateLoggers();
        }
    }
}

测试

    @Test
    public void test_modifying_currentConfiguration_after_initialization_by_extending_standardConfigurationClass() {
        final String relativeFileLocation = "log4j2_config_31_initialConfigFile.xml";
        setLog4j2ConfigFile(relativeFileLocation);

        /*Before modification*/
        printSeparator("Before modification");
        final Logger logger1 = LogManager.getLogger("logger1");
        logger1.fatal("This is a fatal message");

        /*TODO: register the configuration*/
        final LoggerContext loggerContext = (LoggerContext) LogManager.getContext();
        final CustomXmlConfiguration2_ customXmlConfiguration2_ = new CustomXmlConfiguration2_(loggerContext, ConfigurationSource.fromUri(URI.create(relativeFileLocation)));

        /*After modification*/
        printSeparator("After modification");
        final Logger logger2 = LogManager.getLogger("logger2");
        logger2.fatal("This is a fatal message");
        logger1.fatal("This is a fatal message");
    }

问题

如何在测试代码中注册配置?

0 个答案:

没有答案