Skip to content

Commit bf8051c

Browse files
Log ApplicationAvailability state changes
Fixes gh-23098
1 parent 5f95766 commit bf8051c

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ApplicationAvailabilityBean.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
25+
import org.springframework.context.ApplicationEventPublisher;
2226
import org.springframework.context.ApplicationListener;
2327
import org.springframework.util.Assert;
2428

@@ -34,6 +38,8 @@
3438
public class ApplicationAvailabilityBean
3539
implements ApplicationAvailability, ApplicationListener<AvailabilityChangeEvent<?>> {
3640

41+
private static final Log logger = LogFactory.getLog(ApplicationAvailability.class);
42+
3743
private final Map<Class<? extends AvailabilityState>, AvailabilityChangeEvent<?>> events = new HashMap<>();
3844

3945
@Override
@@ -58,10 +64,31 @@ public <S extends AvailabilityState> AvailabilityChangeEvent<S> getLastChangeEve
5864

5965
@Override
6066
public void onApplicationEvent(AvailabilityChangeEvent<?> event) {
67+
logStateChange(event);
6168
Class<? extends AvailabilityState> stateType = getStateType(event.getState());
6269
this.events.put(stateType, event);
6370
}
6471

72+
private void logStateChange(AvailabilityChangeEvent<?> event) {
73+
Class<? extends AvailabilityState> stateType = getStateType(event.getState());
74+
StringBuilder message = new StringBuilder(
75+
"Application availability state " + stateType.getSimpleName() + " changed");
76+
AvailabilityChangeEvent<? extends AvailabilityState> lastChangeEvent = getLastChangeEvent(stateType);
77+
if (lastChangeEvent != null) {
78+
message.append(" from " + lastChangeEvent.getState());
79+
}
80+
message.append(" to " + event.getState());
81+
if (event.getSource() != null) {
82+
if (event.getSource() instanceof Throwable) {
83+
message.append(": " + event.getSource());
84+
}
85+
else if (!(event.getSource() instanceof ApplicationEventPublisher)) {
86+
message.append(": " + event.getSource().getClass().getName());
87+
}
88+
}
89+
logger.info(message);
90+
}
91+
6592
@SuppressWarnings("unchecked")
6693
private Class<? extends AvailabilityState> getStateType(AvailabilityState state) {
6794
if (state instanceof Enum) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/availability/ApplicationAvailabilityBeanTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616

1717
package org.springframework.boot.availability;
1818

19+
import java.io.IOException;
20+
1921
import org.junit.jupiter.api.BeforeEach;
2022
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.ExtendWith;
2124

25+
import org.springframework.boot.testsupport.system.CapturedOutput;
26+
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
2227
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2328

2429
import static org.assertj.core.api.Assertions.assertThat;
@@ -29,6 +34,7 @@
2934
* @author Brian Clozel
3035
* @author Phillip Webb
3136
*/
37+
@ExtendWith(OutputCaptureExtension.class)
3238
class ApplicationAvailabilityBeanTests {
3339

3440
private AnnotationConfigApplicationContext context;
@@ -87,6 +93,28 @@ void getLastChangeEventWhenEventHasBeenPublishedReturnsPublishedState() {
8793
assertThat(this.availability.getLastChangeEvent(TestState.class)).isNotNull();
8894
}
8995

96+
@Test
97+
void stateChangesAreLogged(CapturedOutput output) {
98+
AvailabilityChangeEvent.publish(this.context, LivenessState.CORRECT);
99+
assertThat(output).contains("Application availability state LivenessState changed to CORRECT\n");
100+
AvailabilityChangeEvent.publish(this.context, LivenessState.BROKEN);
101+
assertThat(output).contains("Application availability state LivenessState changed from CORRECT to BROKEN\n");
102+
}
103+
104+
@Test
105+
void stateChangesAreLoggedWithExceptionSource(CapturedOutput output) {
106+
AvailabilityChangeEvent.publish(this.context, new IOException("connection error"), LivenessState.BROKEN);
107+
assertThat(output).contains("Application availability state LivenessState changed to BROKEN: "
108+
+ "java.io.IOException: connection error\n");
109+
}
110+
111+
@Test
112+
void stateChangesAreLoggedWithOtherSource(CapturedOutput output) {
113+
AvailabilityChangeEvent.publish(this.context, new CustomEventSource(), LivenessState.BROKEN);
114+
assertThat(output).contains("Application availability state LivenessState changed to BROKEN: "
115+
+ CustomEventSource.class.getName() + "\n");
116+
}
117+
90118
enum TestState implements AvailabilityState {
91119

92120
ONE {
@@ -107,4 +135,8 @@ public String test() {
107135

108136
}
109137

138+
static class CustomEventSource {
139+
140+
}
141+
110142
}

0 commit comments

Comments
 (0)