From eba476ba6ec5c9eb4ce80d016ded3f214fa04d89 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 29 Jan 2018 16:37:28 +0100 Subject: [PATCH] Disable LoggersEndpoint if the logging system is disabled Closes gh-11793 --- .../LoggersEndpointAutoConfiguration.java | 26 ++++++++++++++++++- ...LoggersEndpointAutoConfigurationTests.java | 13 +++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LoggersEndpointAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LoggersEndpointAutoConfiguration.java index 9eb8c374afb..cecac6970c5 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LoggersEndpointAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LoggersEndpointAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,11 +19,17 @@ package org.springframework.boot.actuate.autoconfigure.logging; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; import org.springframework.boot.actuate.logging.LoggersEndpoint; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionMessage; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.boot.logging.LoggingSystem; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; +import org.springframework.core.type.AnnotatedTypeMetadata; /** * {@link EnableAutoConfiguration Auto-configuration} for the {@link LoggersEndpoint}. @@ -36,10 +42,28 @@ public class LoggersEndpointAutoConfiguration { @Bean @ConditionalOnBean(LoggingSystem.class) + @Conditional(OnEnabledLoggingSystemCondition.class) @ConditionalOnMissingBean @ConditionalOnEnabledEndpoint public LoggersEndpoint loggersEndpoint(LoggingSystem loggingSystem) { return new LoggersEndpoint(loggingSystem); } + + static class OnEnabledLoggingSystemCondition extends SpringBootCondition { + + @Override + public ConditionOutcome getMatchOutcome(ConditionContext context, + AnnotatedTypeMetadata metadata) { + ConditionMessage.Builder message = ConditionMessage + .forCondition("Logging System"); + String loggingSystem = System.getProperty(LoggingSystem.SYSTEM_PROPERTY); + if (LoggingSystem.NONE.equals(loggingSystem)) { + return ConditionOutcome.noMatch(message.because("system property " + + LoggingSystem.SYSTEM_PROPERTY + " is set to none")); + } + return ConditionOutcome.match(message.because("enabled")); + } + + } } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LoggersEndpointAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LoggersEndpointAutoConfigurationTests.java index 7a2c3bde8a6..f840f7275a6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LoggersEndpointAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LoggersEndpointAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,6 +53,17 @@ public class LoggersEndpointAutoConfigurationTests { .doesNotHaveBean(LoggersEndpoint.class)); } + @Test + public void runWithNoneLoggerEndpointShouldNotHaveEndpointBean() { + this.contextRunner + .withSystemProperties("org.springframework.boot.logging.LoggingSystem=none") + .run((context) -> { + System.out.println(context.getBean(LoggingSystem.class)); + assertThat(context) + .doesNotHaveBean(LoggersEndpoint.class); + }); + } + @Configuration static class LoggingConfiguration {