Move configuration of LogFileWebEndpoint

See gh-10263
This commit is contained in:
Stephane Nicoll 2017-09-14 15:02:46 +02:00
parent d7f3008122
commit 5e7dbe8f4a
5 changed files with 95 additions and 17 deletions

View File

@ -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 {

View File

@ -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;
}
}

View File

@ -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--");
});
}
}

View File

@ -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();

View File

@ -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--");