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;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM