I am trying to create Asynchronous Loggers programmatically using log4j2.
It works fine until a specific point but after that it gives a following error:
2019-12-25 13:36:15,503 Log4j2-TF-1-AsyncLogger[AsyncContext@659e0bfd]-1 ERROR Attempted to append to non-started appender consoleAppender
Also, because of this only partial logs are printed. I mean if total 1000 logs were to be printed, only 800 are printed.
The sample code is like follows:
public void log() {
final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
config = builder.build();
ctx = Configurator.initialize(config);
config = ctx.getConfiguration();
ctx.start(config);
ctx.updateLoggers();
logger = ctx.getLogger(loggerName);
logger.addAppender(attachConsoleAppender(ctx.getConfiguration(), appenderName));
while (counter < 2000) {
logger.error(testMessage + counter);
counter++;
}
closeLogger();
}
private Appender attachConsoleAppender(Configuration config, String appenderName) {
Appender consoleAppender = ConsoleAppender.newBuilder().setConfiguration(config).setName(appenderName)
.withImmediateFlush(true).build();
consoleAppender.start();
return consoleAppender;
}
public void closeLogger() {
config.getLoggerConfig(loggerName).getAppenders().get(appenderName).stop();
config.getLoggerConfig(loggerName).removeAppender(appenderName);
config.removeLogger(loggerName);
ctx.updateLoggers();
}
}
The issue according to me is that the asynchronous logger runs in its separate thread than main.
And the logic with which I remove the logger is executed synchronously and it gets executed first which results in closing the appender and removing the logger while logging is still not finished in the other thread.
I have gone through multiple links but could not get much help :
Log4j2 Custom appender: ERROR Attempted to append to non-started appender
log4j:ERROR Attempted to append to closed appender named [..]
log4j: log4j:ERROR Attempted to append to closed appender named [stdout]
https://issues.apache.org/jira/browse/LOG4J2-927
Is there any way to handle this situation, Will it be be good to still wait for the main thread till logging is finished or log the messages even if main thread is finished.And how can we achieve it.
PS: The above code is just sample in which the issue can be replicated.
I get Asynchronous logger with the following property in log4j2.component.properties :
Log4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector