accesslog在springboot中开启,表达式介绍,配置详情,使用undertow容器时%D接口耗时不打印问题解决。

简介:

1.为了接入接口访问监控,可以直接使用容器(undertow,tomcat)自带的配置实现打印accesslog。

配置文件配置(springboot项目使用的容器默认是tomcat那就选2,如果是undertow就选1):

1.undertow容器:

server:
  port: 8081
  undertow:
    accesslog:
      enabled: true   # true 开启accesslog,false:不开启
      dir: /midware/logs/dataapi-service  # 日志打印文件地址
      prefix: dataapi_access.   # 日志打印文件名称
      file-date-format: .yyyy-MM-dd
      pattern: %t [%I] %{X-Forwarded-For}i %a %r %q %s (%D ms)  # 日志输出表达式,默认值是common
      rotate: true    # 为true时,上面的日期格式才有意义,否则就是写在一个文件里了

2.tomcat容器:

server:
  port: 8081
  tomcat:
    accesslog:
      enabled: true   # true 开启accesslog,false:不开启
      dir: /midware/logs/dataapi-service  # 日志打印文件地址
      prefix: dataapi_access.   # 日志打印文件名称
      file-date-format: .yyyy-MM-dd
      pattern: %t [%I] %{X-Forwarded-For}i %a %r %q %s (%D ms)  # 日志输出表达式,默认值是common
      rotate: true    # 为true时,上面的日期格式才有意义,否则就是写在一个文件里了

配置文件配置日志输出表达式介绍:(server.tomcat.accesslog.pattern 解释:)

  • %a - Remote IP address 远程IP地址
  • %A - Local IP address 本地IP地址
  • %b - Bytes sent, excluding HTTP headers, or ‘-’ if no bytes were sent 返回给客户端的数据字节数,- 表示没有数据
  • %B - Bytes sent, excluding HTTP headers 与上面一样,貌似是官方的bug,应该是一个包含 HTTP headers 一个不包含
  • %h - Remote host name (or IP address if enableLookups for the connector is false)
  • %H - Request protocol
  • %l - Remote logical username from identd (always returns ‘-’)
  • %m - Request method
  • %p - Local port
  • %q - Query string (prepended with a ‘?’ if it exists, otherwise an empty string
  • %r - First line of the request
  • %s - HTTP status code of the response
  • %S - User session ID
  • %t - Date and time, in Common Log Format format
  • %u - Remote user that was authenticated
  • %U - Requested URL path
  • %v - Local server name
  • %D - Time taken to process the request, in millis 处理请求耗时,单位毫秒
  • %T - Time taken to process the request, in seconds 同上,单位秒
  • %I - current Request thread name (can compare later with stacktraces) 执行当前请求的线程名称,输出可以统计多少线程在工作,当然也可以通过其他方式,jstack

undertow容器时%D接口耗时不打印问题解决

记录请求所费时间, 所以要在config配置内中加入下面代码,开启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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

酸菜鱼(szy)

如果给您提供帮助的话可以打赏哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值