undertow的日志配置

查看undertow官网: http://undertow.io/undertow-docs/undertow-docs-2.0.0/index.html#access-log-handler

  可知它的日志处理采用    AccessLogHandler类。 

跟踪 AccessLogHandler 类的源码,查看  common和  combined的格式定义.

  new io.undertow.server.handlers.accesslog.AccessLogHandler(new HttpHandler(), new AccessLogReceiver(), "", Test.class.getClassLoader());
private static String handleCommonNames(String formatString) {
        if(formatString.equals("common")) {
            return "%h %l %u %t \"%r\" %s %b";
        } else if (formatString.equals("combined")) {
            return "%h %l %u %t \"%r\" %s %b \"%{i,Referer}\" \"%{i,User-Agent}\"";
        }
        return formatString;
    }

以上呈现的格式字符定义在:   http://undertow.io/undertow-docs/undertow-docs-2.0.0/index.html#exchange-attributes-2

具体分析如下: 

common日志格式: 
   %h %l %u %t \"%r\" %s %b
   %h: 远程主机名
   %l:  远程主机逻辑名: 经常是 -
   %u:   远程受信用户名: 经常是 -
   %t: 日期和时间,用 Common Log Format格式
   %r: Http请求的第一行
   %s: 响应状态码
   %b: 发送的字节数(不包括头域),如是 - 表明没有字节发送, 

combined日志格式: 
   %h %l %u %t \"%r\" %s %b \"%{i,Referer}\" \"%{i,User-Agent}\

   "%{i,Referer}\":   从http请求头中获取 来源地址
   %{i,User-Agent}:   从http请求头中获取 userAgent

我们项目采用: 
   %h %l %u %t \"%r\" %s %b %D \"%{i,Referer}\" \"%{i,User-Agent}\

因为用了  %D,记录请求所费时间, 所以要开启undertow记时:  
 

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

最后我们的配置文件为: 

server:  
    port: 8080
    servlet: 
       session.timeout: 60000
       contextpath: /yc74ibike    
    undertow:  
        io-threads: 16  
        worker-threads: 256  
        buffer-size: 1024  
        buffers-per-region: 1024  
        direct-buffers: true 
        accesslog: 
            dir: ../log/access/
            enabled: true
            pattern: "%h %l %u %t \"%r\" %s %b %D \"%{i,Referer}\" \"%{i,User-Agent}\""
            prefix: ibike_access
            suffix: log
            rotate: true
        
logging:
    level: 
        root: info
        org.springframework: info
        com.yc: info
    file: ../log/run/yc74ibike.log
swagger: 
    enabled: false
    
    


 





 

猜你喜欢

转载自blog.csdn.net/zhangyingchengqi/article/details/107282445