简体   繁体   English

Log4j2:以编程方式删除现有的附加程序并为所有记录器动态添加新的 RollingFileAppender

[英]Log4j2: Programatically remove the existing appenders and add new RollingFileAppender dynamically for all loggers

We have a requirement to add the rolling file appender programmatically in application startup.我们需要在应用程序启动时以编程方式添加滚动文件附加程序。 And also I need to remove the existing appenders(which are configured from log4j2.xml file).而且我还需要删除现有的附加程序(它们是从 log4j2.xml 文件配置的)。 I tried various approaches, but it didnt worked for me.我尝试了各种方法,但对我没有用。

The problems I'm facing now are:我现在面临的问题是:

  1. Some logs are still coming to the old appenders.一些日志仍在旧的附加程序中。
  2. If I update logger level(from INFO to DEBUG ) for any logger package those things also not coming properly.如果我为任何记录器 package 更新记录器级别(从INFODEBUG ),那些事情也不会正常进行。
        LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
        Configuration configuration = loggerContext.getConfiguration();

        //Removing the existing appenders
        configuration.getAppenders().keySet().forEach(((AbstractConfiguration)configuration)::removeAppender);
        
        String rollingAppenderName = "RollAppender";
        RollingFileAppender rollingFileAppender = RollingFileAppender.newBuilder()
                .setConfiguration(configuration)
                .setName(rollingAppenderName)
                .setLayout(layout)
                .withFileName("/tmp/test.log")
                .withFilePattern("/tmp/test.%i.log")
                .withPolicy(SizeBasedTriggeringPolicy.createPolicy("10MB"))
                .build();
        rollingFileAppender.start();
        configuration.addAppender(rollingFileAppender);

        /*
         * Updating appenders of all the loggerConfigs configured in the log4j2 config file.
         * */
        LoggerConfig rootLogger = configuration.getRootLogger();
        

        Map<String, LoggerConfig> loggerMap = configuration.getLoggers();
        for (LoggerConfig loggerConfig : loggerMap.values()) {
            AppenderRef appenderRef = AppenderRef.createAppenderRef(rollingAppenderName, loggerConfig.getLevel(), loggerConfig.getFilter());
            AppenderRef[] refs = new AppenderRef[]{appenderRef};
            if (Objects.equals(loggerConfig.getName(), rootLogger.getName())) {
                loggerConfig = LoggerConfig.RootLogger.createLogger("true", loggerConfig.getLevel(),
                        Boolean.toString(loggerConfig.isIncludeLocation()), refs, null, configuration, loggerConfig.getFilter());
                loggerConfig.addAppender(rollingFileAppender, loggerConfig.getLevel(), loggerConfig.getFilter());
            } else {
                loggerConfig = LoggerConfig.createLogger(true, loggerConfig.getLevel(), loggerConfig.getName(),
                        Boolean.toString(loggerConfig.isIncludeLocation()), refs, null, configuration, loggerConfig.getFilter());
            }

            configuration.removeLogger(loggerConfig.getName());
            configuration.addLogger(loggerConfig.getName(), loggerConfig);
        }

        loggerContext.updateLoggers();

The approach you are taking is incorrect.您采用的方法不正确。 Trying to manipulate the existing configuration in the way you are will result in log events being lost while you are messing with the configuration.尝试以您现在的方式操作现有配置将导致日志事件在您弄乱配置时丢失。 Instead, create a new configuration and once it is constructed replace the old configuration with the new one.相反,创建一个新配置,并在构建后用新配置替换旧配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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