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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.ConditionalOnBean;
|
||||||
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.bind.RelaxedPropertyResolver;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
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;
|
||||||
|
|
@ -180,6 +181,12 @@ public class EndpointWebMvcManagementContextConfiguration {
|
||||||
if (StringUtils.hasText(config)) {
|
if (StringUtils.hasText(config)) {
|
||||||
return ConditionOutcome.match("Found logging.path: " + 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");
|
return ConditionOutcome.noMatch("Found no log file configuration");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.actuate.endpoint.mvc;
|
package org.springframework.boot.actuate.endpoint.mvc;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
|
@ -72,6 +73,12 @@ public class LogFileMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
||||||
*/
|
*/
|
||||||
private Boolean sensitive;
|
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;
|
private Environment environment;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -105,6 +112,14 @@ public class LogFileMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
||||||
this.sensitive = sensitive;
|
this.sensitive = sensitive;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public File getExternalFile() {
|
||||||
|
return this.externalFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setExternalFile(File externalFile) {
|
||||||
|
this.externalFile = externalFile;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("rawtypes")
|
@SuppressWarnings("rawtypes")
|
||||||
public Class<? extends Endpoint> getEndpointType() {
|
public Class<? extends Endpoint> getEndpointType() {
|
||||||
|
|
@ -119,23 +134,25 @@ public class LogFileMvcEndpoint implements MvcEndpoint, EnvironmentAware {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Resource resource = getLogFileResource();
|
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);
|
new Handler(resource).handleRequest(request, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Resource getLogFileResource() {
|
private Resource getLogFileResource() {
|
||||||
|
if (this.externalFile != null) {
|
||||||
|
return new FileSystemResource(this.externalFile);
|
||||||
|
}
|
||||||
LogFile logFile = LogFile.get(this.environment);
|
LogFile logFile = LogFile.get(this.environment);
|
||||||
if (logFile == null) {
|
if (logFile == null) {
|
||||||
logger.debug("Missing 'logging.file' or 'logging.path' properties");
|
logger.debug("Missing 'logging.file' or 'logging.path' properties");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
FileSystemResource resource = new FileSystemResource(logFile.toString());
|
return new FileSystemResource(logFile.toString());
|
||||||
if (!resource.exists()) {
|
|
||||||
if (logger.isDebugEnabled()) {
|
|
||||||
logger.debug("Log file '" + resource + "' does not exist");
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return resource;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -110,4 +110,15 @@ public class LogFileMvcEndpointTests {
|
||||||
assertThat(response.getContentAsString()).isEqualTo("--TEST--");
|
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