Commit 5e7dbe8f authored by Stephane Nicoll's avatar Stephane Nicoll

Move configuration of LogFileWebEndpoint

See gh-10263
parent d7f30081
...@@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionMessage; ...@@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionMessage;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition; 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.Bean;
import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Conditional;
...@@ -36,13 +37,21 @@ import org.springframework.util.StringUtils; ...@@ -36,13 +37,21 @@ import org.springframework.util.StringUtils;
* @since 2.0.0 * @since 2.0.0
*/ */
@ManagementContextConfiguration @ManagementContextConfiguration
@EnableConfigurationProperties(LogFileWebEndpointProperties.class)
public class LogFileWebEndpointManagementContextConfiguration { public class LogFileWebEndpointManagementContextConfiguration {
private final LogFileWebEndpointProperties properties;
public LogFileWebEndpointManagementContextConfiguration(
LogFileWebEndpointProperties properties) {
this.properties = properties;
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@Conditional(LogFileCondition.class) @Conditional(LogFileCondition.class)
public LogFileWebEndpoint logFileWebEndpoint(Environment environment) { public LogFileWebEndpoint logFileWebEndpoint(Environment environment) {
return new LogFileWebEndpoint(environment); return new LogFileWebEndpoint(environment, this.properties.getExternalFile());
} }
private static class LogFileCondition extends SpringBootCondition { private static class LogFileCondition extends SpringBootCondition {
......
/*
* 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;
}
}
...@@ -16,10 +16,19 @@ ...@@ -16,10 +16,19 @@
package org.springframework.boot.actuate.autoconfigure.logging; 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.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.actuate.logger.LogFileWebEndpoint; import org.springframework.boot.actuate.logger.LogFileWebEndpoint;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; 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; import static org.assertj.core.api.Assertions.assertThat;
...@@ -32,6 +41,10 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -32,6 +41,10 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class LogFileWebEndpointManagementContextConfigurationTests { public class LogFileWebEndpointManagementContextConfigurationTests {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withUserConfiguration( .withUserConfiguration(
LogFileWebEndpointManagementContextConfiguration.class); LogFileWebEndpointManagementContextConfiguration.class);
...@@ -65,4 +78,21 @@ public class LogFileWebEndpointManagementContextConfigurationTests { ...@@ -65,4 +78,21 @@ public class LogFileWebEndpointManagementContextConfigurationTests {
.hasSingleBean(LogFileWebEndpoint.class)); .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--");
});
}
} }
...@@ -24,7 +24,6 @@ import org.apache.commons.logging.LogFactory; ...@@ -24,7 +24,6 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.endpoint.EndpointExposure; import org.springframework.boot.actuate.endpoint.EndpointExposure;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.logging.LogFile; import org.springframework.boot.logging.LogFile;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.FileSystemResource;
...@@ -38,7 +37,6 @@ import org.springframework.core.io.Resource; ...@@ -38,7 +37,6 @@ import org.springframework.core.io.Resource;
* @author Andy Wilkinson * @author Andy Wilkinson
* @since 2.0.0 * @since 2.0.0
*/ */
@ConfigurationProperties(prefix = "endpoints.logfile")
@Endpoint(id = "logfile", exposure = EndpointExposure.WEB) @Endpoint(id = "logfile", exposure = EndpointExposure.WEB)
public class LogFileWebEndpoint { public class LogFileWebEndpoint {
...@@ -46,22 +44,15 @@ public class LogFileWebEndpoint { ...@@ -46,22 +44,15 @@ public class LogFileWebEndpoint {
private final Environment environment; 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; private File externalFile;
public LogFileWebEndpoint(Environment environment) { public LogFileWebEndpoint(Environment environment, File externalFile) {
this.environment = environment; this.environment = environment;
this.externalFile = externalFile;
} }
public File getExternalFile() { public LogFileWebEndpoint(Environment environment) {
return this.externalFile; this(environment, null);
}
public void setExternalFile(File externalFile) {
this.externalFile = externalFile;
} }
@ReadOperation @ReadOperation
......
...@@ -78,8 +78,9 @@ public class LogFileWebEndpointTests { ...@@ -78,8 +78,9 @@ public class LogFileWebEndpointTests {
@Test @Test
public void resourceResponseWithExternalLogFile() throws Exception { public void resourceResponseWithExternalLogFile() throws Exception {
this.endpoint.setExternalFile(this.logFile); LogFileWebEndpoint endpoint = new LogFileWebEndpoint(this.environment,
Resource resource = this.endpoint.logFile(); this.logFile);
Resource resource = endpoint.logFile();
assertThat(resource).isNotNull(); assertThat(resource).isNotNull();
assertThat(StreamUtils.copyToString(resource.getInputStream(), assertThat(StreamUtils.copyToString(resource.getInputStream(),
StandardCharsets.UTF_8)).isEqualTo("--TEST--"); StandardCharsets.UTF_8)).isEqualTo("--TEST--");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment