Skip to content

Commit 935fa09

Browse files
committed
feat: Add ability to customize access log prefix for management logs
1 parent 86b0c76 commit 935fa09

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ public class ManagementServerProperties {
5454
*/
5555
private String basePath = "";
5656

57+
/**
58+
* Enable management access logs prefix customization
59+
* management.server.accesslog.prefix.
60+
*/
61+
private String accesslogPrefix = "management_";
62+
5763
@NestedConfigurationProperty
5864
private Ssl ssl;
5965

@@ -117,4 +123,12 @@ private String cleanBasePath(String basePath) {
117123
return candidate;
118124
}
119125

126+
public String getAccesslogPrefix() {
127+
return this.accesslogPrefix;
128+
}
129+
130+
public void setAccesslogPrefix(String accesslogPrefix) {
131+
this.accesslogPrefix = accesslogPrefix;
132+
}
133+
120134
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,23 @@ ServletManagementWebServerFactoryCustomizer servletManagementWebServerFactoryCus
8383

8484
@Bean
8585
@ConditionalOnClass(name = "io.undertow.Undertow")
86-
UndertowAccessLogCustomizer undertowManagementAccessLogCustomizer() {
87-
return new UndertowAccessLogCustomizer();
86+
UndertowAccessLogCustomizer undertowManagementAccessLogCustomizer(
87+
ManagementServerProperties managementServerProperties) {
88+
return new UndertowAccessLogCustomizer(managementServerProperties);
8889
}
8990

9091
@Bean
9192
@ConditionalOnClass(name = "org.apache.catalina.valves.AccessLogValve")
92-
TomcatAccessLogCustomizer tomcatManagementAccessLogCustomizer() {
93-
return new TomcatAccessLogCustomizer();
93+
TomcatAccessLogCustomizer tomcatManagementAccessLogCustomizer(
94+
ManagementServerProperties managementServerProperties) {
95+
return new TomcatAccessLogCustomizer(managementServerProperties);
9496
}
9597

9698
@Bean
9799
@ConditionalOnClass(name = "org.eclipse.jetty.server.Server")
98-
JettyAccessLogCustomizer jettyManagementAccessLogCustomizer() {
99-
return new JettyAccessLogCustomizer();
100+
JettyAccessLogCustomizer jettyManagementAccessLogCustomizer(
101+
ManagementServerProperties managementServerProperties) {
102+
return new JettyAccessLogCustomizer(managementServerProperties);
100103
}
101104

102105
@Configuration(proxyBeanMethods = false)
@@ -147,12 +150,12 @@ abstract static class AccessLogCustomizer implements Ordered {
147150

148151
private static final String MANAGEMENT_PREFIX = "management_";
149152

150-
protected String customizePrefix(String prefix) {
153+
protected String customizePrefix(String prefix, String accessLogPrefix) {
151154
prefix = (prefix != null) ? prefix : "";
152155
if (prefix.startsWith(MANAGEMENT_PREFIX)) {
153156
return prefix;
154157
}
155-
return MANAGEMENT_PREFIX + prefix;
158+
return accessLogPrefix + prefix;
156159
}
157160

158161
@Override
@@ -165,13 +168,20 @@ public int getOrder() {
165168
static class TomcatAccessLogCustomizer extends AccessLogCustomizer
166169
implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
167170

171+
private final ManagementServerProperties managementServerProperties;
172+
173+
TomcatAccessLogCustomizer(ManagementServerProperties managementServerProperties) {
174+
this.managementServerProperties = managementServerProperties;
175+
}
176+
168177
@Override
169178
public void customize(TomcatServletWebServerFactory factory) {
170179
AccessLogValve accessLogValve = findAccessLogValve(factory);
171180
if (accessLogValve == null) {
172181
return;
173182
}
174-
accessLogValve.setPrefix(customizePrefix(accessLogValve.getPrefix()));
183+
184+
accessLogValve.setPrefix(customizePrefix(accessLogValve.getPrefix(), this.managementServerProperties.getAccesslogPrefix()));
175185
}
176186

177187
private AccessLogValve findAccessLogValve(TomcatServletWebServerFactory factory) {
@@ -188,16 +198,28 @@ private AccessLogValve findAccessLogValve(TomcatServletWebServerFactory factory)
188198
static class UndertowAccessLogCustomizer extends AccessLogCustomizer
189199
implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {
190200

201+
private final ManagementServerProperties managementServerProperties;
202+
203+
UndertowAccessLogCustomizer(ManagementServerProperties managementServerProperties) {
204+
this.managementServerProperties = managementServerProperties;
205+
}
206+
191207
@Override
192208
public void customize(UndertowServletWebServerFactory factory) {
193-
factory.setAccessLogPrefix(customizePrefix(factory.getAccessLogPrefix()));
209+
factory.setAccessLogPrefix(customizePrefix(factory.getAccessLogPrefix(), this.managementServerProperties.getAccesslogPrefix()));
194210
}
195211

196212
}
197213

198214
static class JettyAccessLogCustomizer extends AccessLogCustomizer
199215
implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {
200216

217+
private final ManagementServerProperties managementServerProperties;
218+
219+
JettyAccessLogCustomizer(ManagementServerProperties managementServerProperties) {
220+
this.managementServerProperties = managementServerProperties;
221+
}
222+
201223
@Override
202224
public void customize(JettyServletWebServerFactory factory) {
203225
factory.addServerCustomizers(this::customizeServer);
@@ -220,7 +242,7 @@ private void customizeRequestLogWriter(RequestLogWriter writer) {
220242
String filename = writer.getFileName();
221243
if (StringUtils.hasLength(filename)) {
222244
File file = new File(filename);
223-
file = new File(file.getParentFile(), customizePrefix(file.getName()));
245+
file = new File(file.getParentFile(), customizePrefix(file.getName(), this.managementServerProperties.getAccesslogPrefix()));
224246
writer.setFilename(file.getPath());
225247
}
226248
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementServerPropertiesTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,10 @@ void slashOfBasePathIsDefaultValue() {
6868
assertThat(properties.getBasePath()).isEmpty();
6969
}
7070

71+
@Test
72+
void accessLogsArePrefixedByDefault() {
73+
ManagementServerProperties properties = new ManagementServerProperties();
74+
assertThat(properties.getAccesslogPrefix()).isEqualTo("management_");
75+
}
76+
7177
}

0 commit comments

Comments
 (0)