Simon Xianyu created LOG4J2-1346:
------------------------------------

             Summary: Throw exception in Log4jServletContextListener preventing 
jetty-maven-plugin run-forked.
                 Key: LOG4J2-1346
                 URL: https://issues.apache.org/jira/browse/LOG4J2-1346
             Project: Log4j 2
          Issue Type: Bug
          Components: Web/Servlet
    Affects Versions: 2.5
            Reporter: Simon Xianyu
            Priority: Minor


I tried to integrate jacoco in my maven web project.
However it always failed with the following logs:

[WARNING] FAILED 
o.e.j.m.p.JettyWebAppContext@1d831c2{/,file:/D:/works/paypos2/jacoco-spring/src/main/webapp/,UNAVAILABLE}{file:/D:/works/paypos2/jacoco-spring/src/main/webapp/}:
 java.lang.IllegalStateException: C
ontext destroyed before it was initialized.
java.lang.IllegalStateException: Context destroyed before it was initialized.
        at 
org.apache.logging.log4j.web.Log4jServletContextListener.contextDestroyed(Log4jServletContextListener.java:55)

I read the code and found Exception throw in 
org.apache.logging.log4j.web.Log4jServletContextListener.contextDestroyed:

if (this.servletContext == null || this.initializer == null) {
            throw new IllegalStateException("Context destroyed before it was 
initialized.");
        }

This runtime exception fails jetty-maven-plugin web application configuration 
validation.

So I have to write my own Listener to work around: (Log4jWebLifeCycle is not 
public, so I have to use public interface to operate.)

public void contextDestroyed(final ServletContextEvent event) {
//    if (this.servletContext == null || this.initializer == null) {
//      throw new IllegalStateException("Context destroyed before it was 
initialized.");
//    }
//    LOGGER.debug("Log4jServletContextListener ensuring that Log4j shuts down 
properly.");

    if (this.initializer != null) {
      if (this.initializer instanceof Log4jWebSupport) {
        ((Log4jWebSupport)this.initializer).clearLoggerContext(); // the 
application is finished shutting down now
      }
      this.initializer.stop();
    }
  }

I think using a warning log is better than throw an exception in 
contextDestroyed, because this is shutting down stage.




--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to