Merge pull request #4836 from joshiste/issue-4255
* pr/4836: Add external-file-property to LogFileMvcEndpoint
This commit is contained in:
commit
53d9aa60e1
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2016 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.
|
||||
|
|
@ -40,6 +40,7 @@ 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.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
|
|
@ -180,6 +181,12 @@ public class EndpointWebMvcManagementContextConfiguration {
|
|||
if (StringUtils.hasText(config)) {
|
||||
return ConditionOutcome.match("Found logging.path: " + config);
|
||||
}
|
||||
config = new RelaxedPropertyResolver(environment, "endpoints.logfile.")
|
||||
.getProperty("external-file");
|
||||
if (StringUtils.hasText(config)) {
|
||||
return ConditionOutcome
|
||||
.match("Found endpoints.logfile.external-file: " + config);
|
||||
}
|
||||
return ConditionOutcome.noMatch("Found no log file configuration");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.boot.actuate.endpoint.mvc;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
|
|
@ -72,6 +73,12 @@ public class LogFileMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
|||
*/
|
||||
private Boolean sensitive;
|
||||
|
||||
/**
|
||||
* 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 Environment environment;
|
||||
|
||||
@Override
|
||||
|
|
@ -105,6 +112,14 @@ public class LogFileMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
|||
this.sensitive = sensitive;
|
||||
}
|
||||
|
||||
public File getExternalFile() {
|
||||
return this.externalFile;
|
||||
}
|
||||
|
||||
public void setExternalFile(File externalFile) {
|
||||
this.externalFile = externalFile;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("rawtypes")
|
||||
public Class<? extends Endpoint> getEndpointType() {
|
||||
|
|
@ -119,23 +134,25 @@ public class LogFileMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
|||
return;
|
||||
}
|
||||
Resource resource = getLogFileResource();
|
||||
if (resource != null && !resource.exists()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Log file '" + resource + "' does not exist");
|
||||
}
|
||||
resource = null;
|
||||
}
|
||||
new Handler(resource).handleRequest(request, response);
|
||||
}
|
||||
|
||||
private Resource getLogFileResource() {
|
||||
if (this.externalFile != null) {
|
||||
return new FileSystemResource(this.externalFile);
|
||||
}
|
||||
LogFile logFile = LogFile.get(this.environment);
|
||||
if (logFile == null) {
|
||||
logger.debug("Missing 'logging.file' or 'logging.path' properties");
|
||||
return null;
|
||||
}
|
||||
FileSystemResource resource = new FileSystemResource(logFile.toString());
|
||||
if (!resource.exists()) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Log file '" + resource + "' does not exist");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return resource;
|
||||
return new FileSystemResource(logFile.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -110,4 +110,15 @@ public class LogFileMvcEndpointTests {
|
|||
assertThat(response.getContentAsString()).isEqualTo("--TEST--");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invokeGetsContentExternalFile() throws Exception {
|
||||
this.mvc.setExternalFile(this.logFile);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(),
|
||||
"/logfile");
|
||||
this.mvc.invoke(request, response);
|
||||
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
|
||||
assertThat("--TEST--").isEqualTo(response.getContentAsString());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue