Change state change logging to DEBUG
Update `ApplicationAvailabilityBean` so that state change logging is at DEBUG rather than INFO. Fixes gh-26624
This commit is contained in:
@@ -38,10 +38,18 @@ import org.springframework.util.Assert;
|
||||
public class ApplicationAvailabilityBean
|
||||
implements ApplicationAvailability, ApplicationListener<AvailabilityChangeEvent<?>> {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ApplicationAvailabilityBean.class);
|
||||
|
||||
private final Map<Class<? extends AvailabilityState>, AvailabilityChangeEvent<?>> events = new HashMap<>();
|
||||
|
||||
private final Log logger;
|
||||
|
||||
public ApplicationAvailabilityBean() {
|
||||
this(LogFactory.getLog(ApplicationAvailabilityBean.class));
|
||||
}
|
||||
|
||||
ApplicationAvailabilityBean(Log logger) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S extends AvailabilityState> S getState(Class<S> stateType, S defaultState) {
|
||||
Assert.notNull(stateType, "StateType must not be null");
|
||||
@@ -65,8 +73,8 @@ public class ApplicationAvailabilityBean
|
||||
@Override
|
||||
public void onApplicationEvent(AvailabilityChangeEvent<?> event) {
|
||||
Class<? extends AvailabilityState> type = getStateType(event.getState());
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info(getLogMessage(type, event));
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug(getLogMessage(type, event));
|
||||
}
|
||||
this.events.put(type, event);
|
||||
}
|
||||
|
||||
@@ -17,16 +17,22 @@
|
||||
package org.springframework.boot.availability;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
|
||||
import org.springframework.boot.testsupport.system.CapturedOutput;
|
||||
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.willAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link ApplicationAvailabilityBean}
|
||||
@@ -34,17 +40,19 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Brian Clozel
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@ExtendWith(OutputCaptureExtension.class)
|
||||
class ApplicationAvailabilityBeanTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
||||
private ApplicationAvailabilityBean availability;
|
||||
|
||||
private MockLog log;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.context = new AnnotationConfigApplicationContext(ApplicationAvailabilityBean.class);
|
||||
this.context = new AnnotationConfigApplicationContext(TestConfiguration.class);
|
||||
this.availability = this.context.getBean(ApplicationAvailabilityBean.class);
|
||||
this.log = this.context.getBean(MockLog.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -94,27 +102,26 @@ class ApplicationAvailabilityBeanTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateChangesAreLogged(CapturedOutput output) {
|
||||
void stateChangesAreLogged() {
|
||||
AvailabilityChangeEvent.publish(this.context, LivenessState.CORRECT);
|
||||
assertThat(output)
|
||||
.contains("Application availability state LivenessState changed to CORRECT" + System.lineSeparator());
|
||||
assertThat(this.log.getLogged()).contains("Application availability state LivenessState changed to CORRECT");
|
||||
AvailabilityChangeEvent.publish(this.context, LivenessState.BROKEN);
|
||||
assertThat(output).contains(
|
||||
"Application availability state LivenessState changed from CORRECT to BROKEN" + System.lineSeparator());
|
||||
assertThat(this.log.getLogged())
|
||||
.contains("Application availability state LivenessState changed from CORRECT to BROKEN");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateChangesAreLoggedWithExceptionSource(CapturedOutput output) {
|
||||
void stateChangesAreLoggedWithExceptionSource() {
|
||||
AvailabilityChangeEvent.publish(this.context, new IOException("connection error"), LivenessState.BROKEN);
|
||||
assertThat(output).contains("Application availability state LivenessState changed to BROKEN: "
|
||||
+ "java.io.IOException: connection error" + System.lineSeparator());
|
||||
assertThat(this.log.getLogged()).contains("Application availability state LivenessState changed to BROKEN: "
|
||||
+ "java.io.IOException: connection error");
|
||||
}
|
||||
|
||||
@Test
|
||||
void stateChangesAreLoggedWithOtherSource(CapturedOutput output) {
|
||||
void stateChangesAreLoggedWithOtherSource() {
|
||||
AvailabilityChangeEvent.publish(this.context, new CustomEventSource(), LivenessState.BROKEN);
|
||||
assertThat(output).contains("Application availability state LivenessState changed to BROKEN: "
|
||||
+ CustomEventSource.class.getName() + System.lineSeparator());
|
||||
assertThat(this.log.getLogged()).contains(
|
||||
"Application availability state LivenessState changed to BROKEN: " + CustomEventSource.class.getName());
|
||||
}
|
||||
|
||||
enum TestState implements AvailabilityState {
|
||||
@@ -141,4 +148,30 @@ class ApplicationAvailabilityBeanTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class TestConfiguration {
|
||||
|
||||
@Bean
|
||||
MockLog mockLog() {
|
||||
List<String> logged = new ArrayList<>();
|
||||
MockLog log = mock(MockLog.class);
|
||||
given(log.isDebugEnabled()).willReturn(true);
|
||||
given(log.getLogged()).willReturn(logged);
|
||||
willAnswer((invocation) -> logged.add("" + invocation.getArguments()[0])).given(log).debug(any());
|
||||
return log;
|
||||
}
|
||||
|
||||
@Bean
|
||||
ApplicationAvailabilityBean applicationAvailabilityBean(MockLog log) {
|
||||
return new ApplicationAvailabilityBean(log);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface MockLog extends Log {
|
||||
|
||||
List<String> getLogged();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user