Prevent restarts from switching off Log4J2-based logging
During a restart, the Restarter runs all registered shutdown hooks. This breaks Log4J2 as it leaves it in a shutdown state that leaves logging switched off such that no output it produced when the application starts up again. This commit introduces a new RestartListener abstraction. RestartListeners are notified prior to the application being restarted. A Log4J2-specific implementation is provided that prepares Log4J2 for restart by removing any shutdown callbacks from its shutdown callback registry. This prevents the restart from shutting down Log4J2, ensuring that it still functions when the application restarts. Closes gh-4279
This commit is contained in:
@@ -96,6 +96,7 @@ public class RestarterTests {
|
||||
String output = this.out.toString();
|
||||
assertThat(StringUtils.countOccurrencesOf(output, "Tick 0"), greaterThan(1));
|
||||
assertThat(StringUtils.countOccurrencesOf(output, "Tick 1"), greaterThan(1));
|
||||
assertThat(TestRestartListener.restarts, greaterThan(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -214,7 +215,8 @@ public class RestarterTests {
|
||||
}
|
||||
|
||||
public static void main(String... args) {
|
||||
Restarter.initialize(args, false, new MockRestartInitializer());
|
||||
Restarter.initialize(args, false, new MockRestartInitializer(), true,
|
||||
new TestRestartListener());
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
||||
SampleApplication.class);
|
||||
context.registerShutdownHook();
|
||||
@@ -276,4 +278,15 @@ public class RestarterTests {
|
||||
|
||||
}
|
||||
|
||||
private static class TestRestartListener implements RestartListener {
|
||||
|
||||
private static int restarts;
|
||||
|
||||
@Override
|
||||
public void beforeRestart() {
|
||||
restarts++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user