[JAVA] About the solution to the problem that the log of logback is not output when the web application is stopped

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.

Application components

The components (rough components) of the application in which this event occurred are as follows.

Why wasn't the log output?

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.

Who has stopped the log output function of Logback?

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.

How can I output the log?

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.

Disable automatic registration of 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.

<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

Manually add 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.

Related Links

Recommended Posts

About the solution to the problem that the log of logback is not output when the web application is stopped
Logback log is not output when the server application is closed
Use cryptographically secure pseudo-random numbers to verify that the solution to the Monty Hall problem is not 50%
About the problem that the image is not displayed after AWS deployment
Incident (rails) that is logged out when user editing of the application
How to solve the problem that it is not processed normally when nesting beans in Spring Batch
When the hover of Eclipse is hard to see
About the solution of the error that occurred when trying to create a Japanese file of devise in the Docker development environment
I want to solve the problem that JS is not displayed properly unless reloaded when transitioning with Turbolinks: link_to
When changing the Controller of Spring Web MVC to kotlin, @Autowired The specified component is not injected and becomes null.
[Android Studio] About the matter that the design view is not displayed when using TextClock
[Java] The problem that uploaded images are not updated due to the influence of cache
How to fix the problem that the upper half is cut off when using UITabBar
A memo of the program that allows you to realize that the probability of dice rolling is about 1/6
How to solve the problem when the value is not sent when the form is disabled in rails and sent
When there is no output to stdout in docker log
I want to change the log output settings of UtilLoggingJdbcLogger
The idea of cutting off when the error is not resolved
[Rails] When the layout change of devise is not reflected
How to get the log when install4j does not start
How to solve the problem that the website image is not displayed after deploying to heroku on Rails 5
[Firebase] The problem that the currentUser of Firebase Auth does not become null even if the app is uninstalled.
How to fix the problem that Aptana Studio does not start
A solution to the problem of blank spaces at the beginning of textarea
Summary of problems that I could not log in to firebase
How to output the value when there is an array in the array
Summary: Implementation of a function that switches the logical value when a link is clicked [for own output]
[WSL] Solution for the phenomenon that 404 is displayed when trying to insert Java with apt (personal memo)
Perform a large amount of csv export (output) of log information etc. on the WEB application with Rails application
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
[Rails] How to solve the problem that the default image is overwritten when editing without uploading the image [Active Storage]
About the matter that tends to be confused with ARG of Dockerfile which is a multi-stage build
About the log level of java.util.logging.Logger
About the problem that the server can not be started with rails s
[Rails] About the error when displaying the screen due to the autofocus of the form
To you who lament that the conversion of JODConverter + LibreOffice is slow
Perspectives to worry about when doing code reviews of web applications (Rails)
[Rails] About the error that the image is not displayed in the production environment
Solution of component that saves cookie when Talend WebAPI data is acquired
Problem of not being able to db: migrate when adding Sorcery submodules
How to constrain the action of the transition destination when not logged in
The story that the Servlet could not be loaded in the Java Web application
ActiveRecord prints the SQL query that is actually issued to standard output
[Rails] What to do when the Refile image is not displayed when writing the processing at the time of Routing Error
I tried to make a web application that searches tweets with vue-word cloud and examines the tendency of what is written in the associated profile
What is testing? ・ About the importance of testing
Output HTTP header of google-http-client to log
Output of the book "Introduction to Java"
About the mechanism of the Web and HTTP
[swift] How to control the behavior when the back button of NavigationBar is pressed
Possibility when deploying to EC2 but nothing is displayed in the error log
[Ruby] Meaning of &. How to avoid the error when the receiver (object) is nil
Upcast (Java) that can reduce the amount of change when the specification is changed
How to set when "The constructor Empty () is not visible" occurs in junit
Is it mainstream not to write the closing tag of <P> tag in Javadoc?
[Rails] About the problem that the video thumbnail automatically created by FFmpeg is not displayed [AWS S3 + EC2 + CarrierWave + Fog]
[Swift] If the support of the application is iOS 11 or later, it was not necessary to use Int and Int64 properly