I am using log4j2 v2.17.2, and need to programmatically add loggers/appenders at runtime. I am creating LoggerConfig as shown in: https://logging.apache.org/log4j/2.x/manual/customconfig.html Section : Programmatically Modifying the Current Configuration after Initialization
including this snippet:
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
final Layout layout = PatternLayout.createDefaultLayout(config);
Appender appender = FileAppender.createAppender("target/test.log", "false", "false", "File", "true",
"false", "false", "4000", layout, null, "false", null, config);
appender.start();
config.addAppender(appender);
AppenderRef ref = AppenderRef.createAppenderRef("File", null, null);
AppenderRef[] refs = new AppenderRef[] {ref};
LoggerConfig loggerConfig = LoggerConfig.createLogger("false", "info", "org.apache.logging.log4j",
"true", refs, null, config, null );
loggerConfig.addAppender(appender, null, null);
config.addLogger("org.apache.logging.log4j", loggerConfig);
ctx.updateLoggers();
When creating the LoggerConfig with : LoggerConfig.createLogger(..) (Now I do it with builders) we pass the Appender "refs" that includes a new Appender reference. I checked, and my logging works with that new LoggerConfig even without passing any object to "refs", meaning "refs" is an empty array, so my question is, why are we instructed to add it to new loggerConfig?
When we create loggerConfig in static/file configuration, we need to add the Appender references to the tag (which is analogue to LoggerConfig object) so it seems needed, but programmatically we add the appender directly to the LoggerConfig and it seems to work as well. Thanks.