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 11, 2024
1 parent 86b0c76 commit 86f1af1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class ManagementServerProperties {
@NestedConfigurationProperty
private Ssl ssl;

private final Accesslog accesslog = new Accesslog();

/**
* Returns the management port or {@code null} if the
* {@link ServerProperties#getPort() server port} should be used.
Expand Down Expand Up @@ -117,4 +119,26 @@ private String cleanBasePath(String basePath) {
return candidate;
}

public Accesslog getAccesslog() {
return this.accesslog;
}

public static class Accesslog {

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

public String getPrefix() {
return this.prefix;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryCustomizer;
import org.springframework.boot.autoconfigure.web.servlet.TomcatServletWebServerFactoryCustomizer;
import org.springframework.boot.autoconfigure.web.servlet.UndertowServletWebServerFactoryCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
Expand All @@ -73,6 +74,7 @@
*/
@ManagementContextConfiguration(value = ManagementContextType.CHILD, proxyBeanMethods = false)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(ManagementServerProperties.class)
class ServletManagementChildContextConfiguration {

@Bean
Expand All @@ -83,20 +85,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 @@ -145,14 +149,18 @@ private String getContextPath(ManagementServerProperties managementServerPropert

abstract static class AccessLogCustomizer implements Ordered {

private static final String MANAGEMENT_PREFIX = "management_";
protected final ManagementServerProperties managementServerProperties;

protected String customizePrefix(String prefix) {
AccessLogCustomizer(ManagementServerProperties managementServerProperties) {
this.managementServerProperties = managementServerProperties;
}

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

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

TomcatAccessLogCustomizer(ManagementServerProperties managementServerProperties) {
super(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.getAccesslog().getPrefix()));
}

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

UndertowAccessLogCustomizer(ManagementServerProperties managementServerProperties) {
super(managementServerProperties);
}

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

}

static class JettyAccessLogCustomizer extends AccessLogCustomizer
implements WebServerFactoryCustomizer<JettyServletWebServerFactory> {

JettyAccessLogCustomizer(ManagementServerProperties managementServerProperties) {
super(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.getAccesslog().getPrefix()));
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.getAccesslog().getPrefix()).isEqualTo("management_");
}

}

0 comments on commit 86f1af1

Please sign in to comment.