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

log4j-web deinitalizes the Logger too early if listeners defined in web.xml use it

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.6.2
    • 3.0.0, 2.14.1
    • Web/Servlet
    • None
    • Java8, Linux/Mac, Tomcat

    Description

      We use log4j in a Apache Tomcat environment.
      In the web.xml file we define a custom ServerStartup listener that initializes the DB etc and logs both in contextInitialized and contextDestroyed.

      If we use the log4j-web package the class Log4jServletContainerInitializer initializes the logger and adds a Log4jServletContextListener to the ServletContext. Since this listener is added after our listener the method Log4jServletContextListener#contextDestroyed is called before our listener's contextDestroyed method. Thus the Logger is de-initialized too early.

      We could disable auto initialization and initialize the logger ourselves but then doing it in the listener is too late since the WebService-framework (Metro glassfish) initializes the webservice endpoints before the listener. Since we use static logger variables in those webservice classes this would trigger the message "StatusLogger no log4j configuration found..." because the logger was not yet initialized.

      One possible patch we see is that we make an option to manually add the Log4jServletContextListener:
      In Log4jServletContainerInitializer we would put an "if (! Manually added)" around the call "servletContext.addListener(new Log4jServletContextListener());" and then we could manually add it to the listeners in web.xml

      If you have any other suggestions I would be glad to hear them.

      Attachments

        Issue Links

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            lukedirtwalker Lukas Vogel
            Votes:
            4 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment