简体   繁体   中英

How can we make main thread to wait until asynchronous logging is finished (or other way around)

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

I could see that you have added all global variables that makes when a thread excuted closeLogger() function , it is removing the appender from loggerconfig that makes subsequent threads to failed to write to logs.

You check for active threads before removing the appender hope this helps.

替换您的 closeLogger() 方法或在org.apache.logging.log4j.LogManager.shutdown()之前添加此调用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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