Skip to content

Commit

Permalink
feat: Add ability to customize access log prefix for management logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalourdio committed Dec 10, 2024
1 parent 86b0c76 commit c470bbd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public class ManagementServerProperties {
*/
private String basePath = "";

/**
* Enable management access logs prefix customization
* management.server.accesslog.prefix.
*/
private String accesslogPrefix = "management_";

@NestedConfigurationProperty
private Ssl ssl;

Expand Down Expand Up @@ -117,4 +123,12 @@ private String cleanBasePath(String basePath) {
return candidate;
}

public String getAccesslogPrefix() {
return this.accesslogPrefix;
}

public void setAccesslogPrefix(String accesslogPrefix) {
this.accesslogPrefix = accesslogPrefix;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,22 @@ ServletManagementWebServerFactoryCustomizer servletManagementWebServerFactoryCus

@Bean
@ConditionalOnClass(name = "io.undertow.Undertow")
UndertowAccessLogCustomizer undertowManagementAccessLogCustomizer() {
return new UndertowAccessLogCustomizer();
UndertowAccessLogCustomizer undertowManagementAccessLogCustomizer(
ManagementServerProperties managementServerProperties) {
return new UndertowAccessLogCustomizer(managementServerProperties);
}

@Bean
@ConditionalOnClass(name = "org.apache.catalina.valves.AccessLogValve")
TomcatAccessLogCustomizer tomcatManagementAccessLogCustomizer() {
return new TomcatAccessLogCustomizer();
TomcatAccessLogCustomizer tomcatManagementAccessLogCustomizer(
ManagementServerProperties managementServerProperties) {
return new TomcatAccessLogCustomizer(managementServerProperties);
}

@Bean
@ConditionalOnClass(name = "org.eclipse.jetty.server.Server")
JettyAccessLogCustomizer jettyManagementAccessLogCustomizer() {
return new JettyAccessLogCustomizer();
JettyAccessLogCustomizer jettyManagementAccessLogCustomizer(ManagementServerProperties managementServerProperties) {
return new JettyAccessLogCustomizer(managementServerProperties);
}

@Configuration(proxyBeanMethods = false)
Expand Down Expand Up @@ -147,12 +149,12 @@ abstract static class AccessLogCustomizer implements Ordered {

private static final String MANAGEMENT_PREFIX = "management_";

protected String customizePrefix(String prefix) {
protected String customizePrefix(String prefix, String accessLogPrefix) {
prefix = (prefix != null) ? prefix : "";
if (prefix.startsWith(MANAGEMENT_PREFIX)) {
return prefix;
}
return MANAGEMENT_PREFIX + prefix;
return accessLogPrefix + prefix;
}

@Override
Expand All @@ -165,13 +167,21 @@ public int getOrder() {
static class TomcatAccessLogCustomizer extends AccessLogCustomizer
implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

private final ManagementServerProperties managementServerProperties;

TomcatAccessLogCustomizer(ManagementServerProperties managementServerProperties) {
this.managementServerProperties = managementServerProperties;
}

@Override
public void customize(TomcatServletWebServerFactory factory) {
AccessLogValve accessLogValve = findAccessLogValve(factory);
if (accessLogValve == null) {
return;
}
accessLogValve.setPrefix(customizePrefix(accessLogValve.getPrefix()));

accessLogValve.setPrefix(
customizePrefix(accessLogValve.getPrefix(), this.managementServerProperties.getAccesslogPrefix()));
}

private AccessLogValve findAccessLogValve(TomcatServletWebServerFactory factory) {
Expand All @@ -188,16 +198,29 @@ private AccessLogValve findAccessLogValve(TomcatServletWebServerFactory factory)
static class UndertowAccessLogCustomizer extends AccessLogCustomizer
implements WebServerFactoryCustomizer<UndertowServletWebServerFactory> {

private final ManagementServerProperties managementServerProperties;

UndertowAccessLogCustomizer(ManagementServerProperties managementServerProperties) {
this.managementServerProperties = managementServerProperties;
}

@Override
public void customize(UndertowServletWebServerFactory factory) {
factory.setAccessLogPrefix(customizePrefix(factory.getAccessLogPrefix()));
factory.setAccessLogPrefix(customizePrefix(factory.getAccessLogPrefix(),
this.managementServerProperties.getAccesslogPrefix()));
}

}

static class JettyAccessLogCustomizer extends AccessLogCustomizer
implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {

private final ManagementServerProperties managementServerProperties;

JettyAccessLogCustomizer(ManagementServerProperties managementServerProperties) {
this.managementServerProperties = managementServerProperties;
}

@Override
public void customize(JettyServletWebServerFactory factory) {
factory.addServerCustomizers(this::customizeServer);
Expand All @@ -220,7 +243,8 @@ private void customizeRequestLogWriter(RequestLogWriter writer) {
String filename = writer.getFileName();
if (StringUtils.hasLength(filename)) {
File file = new File(filename);
file = new File(file.getParentFile(), customizePrefix(file.getName()));
file = new File(file.getParentFile(),
customizePrefix(file.getName(), this.managementServerProperties.getAccesslogPrefix()));
writer.setFilename(file.getPath());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ void slashOfBasePathIsDefaultValue() {
assertThat(properties.getBasePath()).isEmpty();
}

@Test
void accessLogsArePrefixedByDefault() {
ManagementServerProperties properties = new ManagementServerProperties();
assertThat(properties.getAccesslogPrefix()).isEqualTo("management_");
}

}

0 comments on commit c470bbd

Please sign in to comment.