Log4j2 Appender Doesn't Close Properly
See original GitHub issueAfter adding the HttpEventCollectorLog4jAppender
to the project, my app hangs indefinitely after finishing execution because the appender is not closed correctly.
Calling LogManager.shutdown()
does not help, since HttpEventCollectorLog4jAppender
overrides the LifeCycle::stop()
method, which never gets called by the LoggerContext
. Since the HttpEventCollectorLog4jAppender
indirectly implements the LifeCycle2
interface, the LifeCycle2::stop(long, TimeUnit)
method is called instead (see LoggerContext::stop(long, TimeUnit)
, which is called indirectly by the LogManger::shutdown()
method).
Calling the stop()
method on the HttpEventCollectorLog4jAppender
“solves” the issue but also presents problems, since the shutdown doesn’t wait for the async post to complete. If there is a log statement right before calling the stop()
method, this message will not be logged to Splunk.
Sample code:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
public class Program {
private static final Logger logger = LogManager.getLogger(Program.class);
public static void main(String[] args) {
logger.info("This won't get logged to Splunk");
LoggerContext.getContext(false)
.getRootLogger()
.getAppenders()
.forEach((name, appender) -> appender.stop());
}
}
The only way to truly shut down the app correctly is by adding Thread.sleep(1000);
before the call to stop()
. This is hacky at best.
Note: My use case is an Apache Spark job, so it has to terminate in a timely manner.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:12 (4 by maintainers)
Version 1.9.0 is also not sending all messages, when Java exits. I could solve the issue, by calling
LoggerContext.getContext(false).stop();
before the application shuts down. Hereby, all log messages are sent to splunk. I hope this helps someone…@kgrabowski, This issue has already been taken care in above PR.
Hence closing this issue. Please let us know in case of any further queries. Thank you.