Skip to content

Commit 53d9aa6

Browse files
committed
Merge pull request #4836 from joshiste/issue-4255
* pr/4836: Add external-file-property to LogFileMvcEndpoint
2 parents 79c163c + 49ef936 commit 53d9aa6

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/EndpointWebMvcManagementContextConfiguration.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,7 @@
4040
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
4141
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4242
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
43+
import org.springframework.boot.bind.RelaxedPropertyResolver;
4344
import org.springframework.boot.context.properties.EnableConfigurationProperties;
4445
import org.springframework.context.annotation.Bean;
4546
import org.springframework.context.annotation.ConditionContext;
@@ -180,6 +181,12 @@ public ConditionOutcome getMatchOutcome(ConditionContext context,
180181
if (StringUtils.hasText(config)) {
181182
return ConditionOutcome.match("Found logging.path: " + config);
182183
}
184+
config = new RelaxedPropertyResolver(environment, "endpoints.logfile.")
185+
.getProperty("external-file");
186+
if (StringUtils.hasText(config)) {
187+
return ConditionOutcome
188+
.match("Found endpoints.logfile.external-file: " + config);
189+
}
183190
return ConditionOutcome.noMatch("Found no log file configuration");
184191
}
185192

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/LogFileMvcEndpoint.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.actuate.endpoint.mvc;
1818

19+
import java.io.File;
1920
import java.io.IOException;
2021

2122
import javax.servlet.ServletException;
@@ -72,6 +73,12 @@ public class LogFileMvcEndpoint implements MvcEndpoint, EnvironmentAware {
7273
*/
7374
private Boolean sensitive;
7475

76+
/**
77+
* External Logfile to be accessed. Can be used if the logfile is written by output
78+
* redirect and not by the logging-system itself.
79+
*/
80+
private File externalFile;
81+
7582
private Environment environment;
7683

7784
@Override
@@ -105,6 +112,14 @@ public void setSensitive(Boolean sensitive) {
105112
this.sensitive = sensitive;
106113
}
107114

115+
public File getExternalFile() {
116+
return this.externalFile;
117+
}
118+
119+
public void setExternalFile(File externalFile) {
120+
this.externalFile = externalFile;
121+
}
122+
108123
@Override
109124
@SuppressWarnings("rawtypes")
110125
public Class<? extends Endpoint> getEndpointType() {
@@ -119,23 +134,25 @@ public void invoke(HttpServletRequest request, HttpServletResponse response)
119134
return;
120135
}
121136
Resource resource = getLogFileResource();
137+
if (resource != null && !resource.exists()) {
138+
if (logger.isDebugEnabled()) {
139+
logger.debug("Log file '" + resource + "' does not exist");
140+
}
141+
resource = null;
142+
}
122143
new Handler(resource).handleRequest(request, response);
123144
}
124145

125146
private Resource getLogFileResource() {
147+
if (this.externalFile != null) {
148+
return new FileSystemResource(this.externalFile);
149+
}
126150
LogFile logFile = LogFile.get(this.environment);
127151
if (logFile == null) {
128152
logger.debug("Missing 'logging.file' or 'logging.path' properties");
129153
return null;
130154
}
131-
FileSystemResource resource = new FileSystemResource(logFile.toString());
132-
if (!resource.exists()) {
133-
if (logger.isDebugEnabled()) {
134-
logger.debug("Log file '" + resource + "' does not exist");
135-
}
136-
return null;
137-
}
138-
return resource;
155+
return new FileSystemResource(logFile.toString());
139156
}
140157

141158
/**

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/LogFileMvcEndpointTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,15 @@ public void invokeGetsContent() throws Exception {
110110
assertThat(response.getContentAsString()).isEqualTo("--TEST--");
111111
}
112112

113+
@Test
114+
public void invokeGetsContentExternalFile() throws Exception {
115+
this.mvc.setExternalFile(this.logFile);
116+
MockHttpServletResponse response = new MockHttpServletResponse();
117+
MockHttpServletRequest request = new MockHttpServletRequest(HttpMethod.GET.name(),
118+
"/logfile");
119+
this.mvc.invoke(request, response);
120+
assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
121+
assertThat("--TEST--").isEqualTo(response.getContentAsString());
122+
}
123+
113124
}

0 commit comments

Comments
 (0)