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). I tried various approaches, but it didnt worked for me.
The problems I'm facing now are:
- Some logs are still coming to the old appenders.
- If I update logger level(from INFO to DEBUG) for any logger package those things also not coming properly.
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();