question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Log4j2 Appender Doesn't Close Properly

See original GitHub issue

After 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pattadcommented, Aug 16, 2021

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…

0reactions
bparmar-splunkcommented, Sep 2, 2022

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

RollingFileAppender not clearing log file in log4j2 version 2.5
I have tried with below configuration and it is working fine, except that the latest file will be with out date. the log4j2.properties ......
Read more >
Log4j 2 Appenders - Apache Logging Services
The AsyncAppender should be configured after the appenders it references to allow it to shut down properly.
Read more >
Log4j2 Example Tutorial - Configuration, Levels, Appenders
If Log4j2 doesn't find the configuration file, only Root Logger will be ... LoggerContext; import org.apache.logging.log4j.core.appender.
Read more >
Log4j IOStreams – JIRA Report
Type Key By Status Resolution Fix Version Bug LOG4J2‑2578 Resolved Fixed Log4j‑Audit 1.0.2 Bug LOG4J2‑2564 Carter Kozak Closed Fixed 2.12.0 Bug LOG4J2‑2377 Closed Fixed 3.0.0, 2.11.1...
Read more >
Log4Shell Zero-Day Vulnerability - CVE-2021-44228 - JFrog
How can I completely fix the Log4j Log4shell issue? ... Log4j2 is one of the most popular Java logging frameworks.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found