The other day ... I encountered an event that the log was not output when the Web application was stopped: cry: I will make a note of the cause and solution.
The components (rough components) of the application in which this event occurred are as follows.
The cause was that "the process of stopping the log output function of Logback" was called first in the process (discard process) performed when the Web application was stopped.
Roughly speaking, the process was executed in the following flow.
Logback (logback-classic) includes a class called LogbackServletContainerInitializer
, and when deployed to an application server that supports Servlet 3.0 or higher, the LogbackServletContextListener
class is automatically registered in the Servlet container. I did.
In this case, "Process to stop Logback log output function" was implemented in the contextDestroyed
method of LogbackServletContextListener
.
In this case ...
With such a feeling, it seems that "the process of stopping the log output function of Logback" should be executed at the very end.
LogbackServletContextListener
To change the processing order ... First, disable the automatic registration function of LogbackServletContextListener
. There are several ways to disable it, but this entry will show you how to disable it using the parameters of the Servlet container.
web.xml
<context-param>
<param-name>logbackDisableServletContainerInitializer</param-name>
<param-value>true</param-value>
</context-param>
NOTE:
It can also be disabled using system properties or environment variables.
- http://logback.qos.ch/manual/configuration.html#webShutdownHook
LogbackServletContextListener
After disabling automatic registration, define the LogbackServletContextListener
class before the listener class (ContextLoaderListener
) for initializing / destroying the Spring application context.
<listener>
<listener-class>ch.qos.logback.classic.servlet.LogbackServletContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
By doing this, (for the time being ... in Tomcat), after "Spring application context destruction processing" is performed, "Process to stop Logback's log output function" is executed.
Recommended Posts