-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Log4j2 shutdown before it prints the last messages during graceful shutdown of spring boot application. #11360
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
Comments
Looks like issue related to the log4j2 bug
|
Based on the description above, I've tried to reproduce this using Spring Boot 1.5.12 and have been unable to do so. When I kill the application in such a way that the application context is closed, logging works as expected. Given that it's not clear if this is a problem in Spring Boot or in Log4j2, I can't justify spending more time on this without a sample that reproduces the described problem. @Myslyvchuk if you'd like us to spend time trying to help you, please take the time to provide a sample. |
@wilkinsona I experienced the same problem and I know how to reproduce it. I was willing to make a PR request for the issue, but could not decide which would be the right approach for spring boot and how to write a test that would handle regression. Here some of my findings: To reproduce it The thing is that One possible solution is to subclass the Maybe the built in xml files for log4j2 should also have Another possible solution is to mimic what is done by the logback logging system. It stops and resets the context before loading the default spring boot configuration. To make things more interesting spring boot has its own facility shutdown the logger in a shutdown hook via |
Thanks very much, @tsachev. That's allowed me to reproduce the problem of the logging not appearing. I haven't been able to reproduce the warning about the shutdown hook registration, though. Here's some code that reproduces the logging not appearing: package com.example.demo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Gh11360Application {
public static void main(String[] args) {
SpringApplication.run(Gh11360Application.class, args);
}
@Bean
public SlowClose slowClose() {
return new SlowClose();
}
public static class SlowClose {
private Log logger = LogFactory.getLog(SlowClose.class);
public void close() throws InterruptedException {
Thread.sleep(5000);
logger.info("Closed");
}
}
} With the following dependencies there isn't even a need to kill or CTRL+C the application as a graceful shutdown happens automatically: <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency> |
A workaround is to set the public static void main(String[] args) {
System.setProperty("log4j.shutdownHookEnabled", Boolean.toString(false));
SpringApplication.run(Gh13360Application.class, args);
} I think the right thing for Boot to do is to behave in the same way the Log4j does (or at least is intended to) with |
Uh oh!
There was an error while loading. Please reload this page.
Hi, I'm using spring boot with version 1.5.7.RELEASE. Also I'm using Log4j2 logger with excluding
SimpleLogger
from hateoas, data-jpa and adding dependencies for it.The main problem that U have code this is running in thread and has logger.
So, the problem is that when I'm stopping the server with Crtl+C I'm expecting that logger will output
but instead of that I'm receiving
When I'm deleting all dependencies for Log4j2 and deleting exclusions for logging from starters, it prints me the expected result SHUTDOWN.
As I understood there is a problem with shutdown hook of Log4j2. I found a related issues with Log4j2, and all of them described that it was problem in Log4j2 which would be fixed in 2.6 version. But now it is 2.7 version of Log4j2. How to fix this?
The text was updated successfully, but these errors were encountered: