Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-1346

Exception from Log4jServletContextListener prevents jetty-maven-plugin run-forked.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.5
    • 2.6
    • Web/Servlet
    • None

    Description

      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.

      Attachments

        Activity

          People

            Unassigned Unassigned
            simon_xianyu Simon Xianyu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: