【问题标题】:How to disable management context access log in Spring Boot using undertow如何使用 undertow 在 Spring Boot 中禁用管理上下文访问日志
【发布时间】:2020-07-02 00:47:23
【问题描述】:
我正在使用带有嵌入式 Undertow 的 Spring Boot 2.2.4。
我已经使用server.underdow.accesslog.enabled=true 启用了访问日志,一切正常。
我在设置子上下文的不同端口上使用执行器端点。我确实不希望记录对执行器的请求。目前他们会自动转到management_access.log,其中access. 是我的主要访问日志的前缀。
关于如何禁用该访问日志的任何想法?我知道 Spring 正在通过 Factory 为执行器上下文创建一个单独的 WebServer,但我还没有找到自定义工厂的方法。
【问题讨论】:
标签:
spring-boot
spring-boot-actuator
undertow
access-log
【解决方案1】:
我找到了自己的答案(花了太多时间去做)。
这有点小技巧,但它确实有效:
新的配置类:foo.ManagementConfig
package foo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
@ManagementContextConfiguration
public class ManagementConfig {
@Bean
WebServerFactoryCustomizer<UndertowServletWebServerFactory> actuatorCustomizer(@Value("${management.server.port}") int managementPort) {
return factory -> {
if (managementPort == factory.getPort()) {
factory.setAccessLogEnabled(false);
}
};
}
}
创建了 resources/META-INF/spring.factories 以便被 ManagementContext 拾取:
org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration=foo.ManagementConfig
有点骇人听闻的部分是if 语句。如果它只适用于管理环境,那就太好了,但出于某种原因,它试图同时适用于两者。使用 if 语句,它对主要上下文没有任何作用。
如果 management.server.port 未定义或与主要上下文相同,则会产生意想不到的后果。