-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shutdown of Log4j2 initialized via Log4jServletContainerInitializer happens first not last #1782
Comments
Thank you for noticing this issue. Unfortunately the Servlet API treats listeners as second-class citizens compared to servlets and filters: there is no way to give them a name, overwrite their definition or even check if such a listener is defined. Section 8.2.3 gives some rules on how listeners are ordered and gives a warning:
Unless I am mistaken the order of the listeners is:
so there is no way for Log4j to put their listener first. The best we can do is:
|
That is definitely unfortunate. I think changing the documentation of the |
I merged my PR. Can you check the latest |
I tested the This did not change with the changes you made. (by setting status="trace" in the log4j configuration, I can see that the log4j shutdown happens before all the other context listeners are destroyed). Also initialization still happens twice (minor thing I assume). Once from |
Did you test with a |
No, just using the As you already established given the constraints of the Servlet API it is impossible to do this with So I am not entirely sure what your changes are actually trying to fix since when |
After the change you should be able to drop the |
Yes now I got it. With the current Snapshot version it also works in my setting without 👏 |
Description
Shutdown of Log4j2 system initialized via org.apache.logging.log4j.web.Log4jServletContainerInitializer happens first before all ServletContextListener contextDestroyed methods are called in a servlet container (tomcat).
I would expect Log4j2 to shutdown itself at the very end, so other context listeners have the change to log something during their shutdown procedure.
If I intialize/shutdown log4j2 via org.apache.logging.log4j.web.Log4jServletContextListener and put the context listener at the very top in
web.xml
then log4j2 properly shuts down after every other listener.If I debug
in Log4jServletContainerInitializer, all other listener already exists in the servletContext and the Log4jServletContextListener is added to the very end, thus shutdown happens as the very first.
Configuration
Version: log4j2-web 2.20.0
Operating system: MacOS
JDK: JDK 11 Temurin
Servlet Container:: Tomcat 9.0.73
Logs
N/A
The text was updated successfully, but these errors were encountered: