SpringBoot--Undertow AccessLog 响应时长的打印

server.undertow.accesslog.dir=./logs/access_log  //日志路径
server.undertow.accesslog.enabled=true //开启accesslog
server.undertow.accesslog.pattern=%t %a "%r" %s (%D ms)

但是问题来了,改了以后自己测试发现pattern生效了,但是那个%D竟然打不出来,查看undertow文档
http://undertow.io/undertow-docs/undertow-docs-2.0.0/index.html#access-log-handler

没办法,继续在文档里找线索,搜一下request time看看,然后就搜到了这一条配置:

 

也就是说必须要开启这个配置,才能在访问日志里打印响应时间,至于undertow为什么不默认开启这个配置,是因为开启后会对性能造成影响
在spring boot(2.0)要开启这个配置需要通过代码进行配置:
import io.undertow.Undertow;
import io.undertow.UndertowOptions;
import org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class UndertowConfig {

    @Bean
    public UndertowServletWebServerFactory undertowServletWebServerFactory() {
        UndertowServletWebServerFactory factory = new UndertowServletWebServerFactory();
        factory.addBuilderCustomizers(new UndertowBuilderCustomizer() {
            @Override
            public void customize(Undertow.Builder builder) {
                builder.setServerOption(UndertowOptions.RECORD_REQUEST_START_TIME, true);
            }
        });
        return factory;
    }
}

 

posted @ 2019-04-23 17:55  javaeelwh  阅读(2558)  评论(0编辑  收藏  举报