Description
As discussed in issue LOG4J2-1606, a flag was added to address the problem of log4j-web shutting down Log4j when other listeners defined in the web app still want to log messages during their contextDestroyed. However, this is not documented anywhere and it was quite difficult for me to find the solution. I suggest documenting this in Using Log4j 2 in Web Applications or Frequently Asked Questions:
When your web application includes log4j-web (as it should) and you would like to use Log4j to log messages when one of your listener's contextDestroyed
method is called, include the following near the top of your web.xml
:
<context-param>
<!-- auto-shutdown stops log4j when the web fragment unloads, but that is too early because it is before
the listeners shut down. To compensate, use a Log4jShutdownOnContextDestroyedListener and register it before
any other listeners which means it will shut down *after* all other listeners. -->
<param-name>isLog4jAutoShutdownDisabled</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<!-- ensure logging stops after other listeners by registering the shutdown listener first -->
<listener-class>org.apache.logging.log4j.web.Log4jShutdownOnContextDestroyedListener</listener-class>
</listener>
Note that for Servlet 3.x, you don't need to include your listener in your web.xml
if it is annotated with @WebListener
. The above solution will still work for you.
Metadata
Metadata
Assignees
Type
Projects
Status