diff --git a/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointManagementContextConfiguration.java b/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointManagementContextConfiguration.java index e8cab2d5434..fdae46504b0 100644 --- a/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointManagementContextConfiguration.java +++ b/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointManagementContextConfiguration.java @@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionMessage; import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; @@ -36,13 +37,21 @@ import org.springframework.util.StringUtils; * @since 2.0.0 */ @ManagementContextConfiguration +@EnableConfigurationProperties(LogFileWebEndpointProperties.class) public class LogFileWebEndpointManagementContextConfiguration { + private final LogFileWebEndpointProperties properties; + + public LogFileWebEndpointManagementContextConfiguration( + LogFileWebEndpointProperties properties) { + this.properties = properties; + } + @Bean @ConditionalOnMissingBean @Conditional(LogFileCondition.class) public LogFileWebEndpoint logFileWebEndpoint(Environment environment) { - return new LogFileWebEndpoint(environment); + return new LogFileWebEndpoint(environment, this.properties.getExternalFile()); } private static class LogFileCondition extends SpringBootCondition { diff --git a/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointProperties.java b/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointProperties.java new file mode 100644 index 00000000000..11a56c9d150 --- /dev/null +++ b/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointProperties.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.autoconfigure.logging; + +import java.io.File; + +import org.springframework.boot.actuate.logger.LogFileWebEndpoint; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for {@link LogFileWebEndpoint}. + * + * @author Stephane Nicoll + * @since 2.0.0 + */ +@ConfigurationProperties(prefix = "endpoints.logfile") +public class LogFileWebEndpointProperties { + + /** + * External Logfile to be accessed. Can be used if the logfile is written by output + * redirect and not by the logging system itself. + */ + private File externalFile; + + public File getExternalFile() { + return this.externalFile; + } + + public void setExternalFile(File externalFile) { + this.externalFile = externalFile; + } + +} diff --git a/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointManagementContextConfigurationTests.java b/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointManagementContextConfigurationTests.java index ec4995b9dfa..df605d28b00 100644 --- a/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointManagementContextConfigurationTests.java +++ b/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointManagementContextConfigurationTests.java @@ -16,10 +16,19 @@ package org.springframework.boot.actuate.autoconfigure.logging; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import org.springframework.boot.actuate.logger.LogFileWebEndpoint; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.core.io.Resource; +import org.springframework.util.FileCopyUtils; +import org.springframework.util.StreamUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -32,6 +41,10 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class LogFileWebEndpointManagementContextConfigurationTests { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withUserConfiguration( LogFileWebEndpointManagementContextConfiguration.class); @@ -65,4 +78,21 @@ public class LogFileWebEndpointManagementContextConfigurationTests { .hasSingleBean(LogFileWebEndpoint.class)); } + @Test + public void logFileWebEndpointUsesConfiguredExternalFile() throws IOException { + File file = this.temp.newFile("logfile"); + FileCopyUtils.copy("--TEST--" .getBytes(), file); + this.contextRunner + .withPropertyValues("endpoints.logfile.external-file:" + + file.getAbsolutePath()).run((context) -> { + assertThat(context).hasSingleBean(LogFileWebEndpoint.class); + LogFileWebEndpoint endpoint = context.getBean(LogFileWebEndpoint.class); + Resource resource = endpoint.logFile(); + assertThat(resource).isNotNull(); + assertThat(StreamUtils.copyToString(resource.getInputStream(), + StandardCharsets.UTF_8)).isEqualTo("--TEST--"); + }); + + } + } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/logger/LogFileWebEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/logger/LogFileWebEndpoint.java index fb8f5d37871..d7fea69e6d6 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/logger/LogFileWebEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/logger/LogFileWebEndpoint.java @@ -24,7 +24,6 @@ import org.apache.commons.logging.LogFactory; import org.springframework.boot.actuate.endpoint.EndpointExposure; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; -import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.logging.LogFile; import org.springframework.core.env.Environment; import org.springframework.core.io.FileSystemResource; @@ -38,7 +37,6 @@ import org.springframework.core.io.Resource; * @author Andy Wilkinson * @since 2.0.0 */ -@ConfigurationProperties(prefix = "endpoints.logfile") @Endpoint(id = "logfile", exposure = EndpointExposure.WEB) public class LogFileWebEndpoint { @@ -46,24 +44,17 @@ public class LogFileWebEndpoint { private final Environment environment; - /** - * External Logfile to be accessed. Can be used if the logfile is written by output - * redirect and not by the logging system itself. - */ private File externalFile; - public LogFileWebEndpoint(Environment environment) { + public LogFileWebEndpoint(Environment environment, File externalFile) { this.environment = environment; - } - - public File getExternalFile() { - return this.externalFile; - } - - public void setExternalFile(File externalFile) { this.externalFile = externalFile; } + public LogFileWebEndpoint(Environment environment) { + this(environment, null); + } + @ReadOperation public Resource logFile() { Resource logFileResource = getLogFileResource(); diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logger/LogFileWebEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logger/LogFileWebEndpointTests.java index 92e14a25feb..a42fad882c4 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logger/LogFileWebEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/logger/LogFileWebEndpointTests.java @@ -78,8 +78,9 @@ public class LogFileWebEndpointTests { @Test public void resourceResponseWithExternalLogFile() throws Exception { - this.endpoint.setExternalFile(this.logFile); - Resource resource = this.endpoint.logFile(); + LogFileWebEndpoint endpoint = new LogFileWebEndpoint(this.environment, + this.logFile); + Resource resource = endpoint.logFile(); assertThat(resource).isNotNull(); assertThat(StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8)).isEqualTo("--TEST--");