From 49ef93602e4db89944a5897fb250121186161d2c Mon Sep 17 00:00:00 2001 From: Johannes Edmeier Date: Thu, 24 Dec 2015 01:06:00 +0100 Subject: [PATCH] Add external-file-property to LogFileMvcEndpoint Add an additional property to LogFileMvcEndpoint to allow log files to be read from an external location. This is helpful when a launch.script is used to start the boot-application and the logfile is written by a redirect of stdout/stderr. Fixes gh-4255 Closes gh-4836 --- ...tWebMvcManagementContextConfiguration.java | 9 ++++- .../endpoint/mvc/LogFileMvcEndpoint.java | 33 ++++++++++++++----- .../endpoint/mvc/LogFileMvcEndpointTests.java | 11 +++++++ 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.java index e9793f9806..b3ac138bd1 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.java @@ -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"); } diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LogFileMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LogFileMvcEndpoint.java index 532896dd2d..338d2a8d85 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LogFileMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LogFileMvcEndpoint.java @@ -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 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()); } /** diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LogFileMvcEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LogFileMvcEndpointTests.java index e85ad77085..772b1ba7bc 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LogFileMvcEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LogFileMvcEndpointTests.java @@ -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()); + } + }