Tomcat中开启Access Log后如何设置处理请求耗时的时间单位

前置条件

  • Spring Boot(3.1.10)
  • 默认使用了内置的 Tomcat(10.1.19)

application.yml配置

server:
  port: 8080
  tomcat:
    accesslog:
      enabled: true
      pattern: "%{yyyy-MM-dd HH:mm:ss}t [%I] %{X-Forwarded-For}i %a %r %q %s (%{ms}T ms)"
      directory: ./
      prefix: access_log
      suffix: .log
    basedir: /logs
    remoteip:
      remote-ip-header: X-Forwarded-For

Tomcat官方文档

链接: https://tomcat.apache.org/tomcat-10.1-doc/config/valve.html#Access_Logging

摘录:

Values for the pattern attribute are made up of literal text strings, combined with pattern identifiers prefixed by the "%" character to cause replacement by the corresponding variable value from the current request and response. The following pattern codes are supported:

%a - Remote IP address. See also %{xxx}a below.
%A - Local IP address
%b - Bytes sent, excluding HTTP headers, or '-' if zero
%B - Bytes sent, excluding HTTP headers
%D - Time taken to process the request in microseconds
%F - Time taken to commit the response, in milliseconds
%h - Remote host name (or IP address if enableLookups for the connector is false)
%H - Request protocol
%I - Current request thread name (can compare later with stacktraces)
%l - Remote logical username from identd (always returns '-')
%m - Request method (GET, POST, etc.)
%p - Local port on which this request was received. See also %{xxx}p below.
%q - Query string (prepended with a '?' if it exists)
%r - First line of the request (method and request URI)
%s - HTTP status code of the response
%S - User session ID
%t - Date and time, in Common Log Format
%T - Time taken to process the request, in seconds
%u - Remote user that was authenticated (if any), else '-' (escaped if required)
%U - Requested URL path
%v - Local server name
%X - Connection status when response is completed:
X = Connection aborted before the response completed.
+ = Connection may be kept alive after the response is sent.
- = Connection will be closed after the response is sent.
There is also support to write information incoming or outgoing headers, cookies, session or request attributes and special timestamp formats. It is modeled after the Apache HTTP Server log configuration syntax. Each of them can be used multiple times with different xxx keys:

%{xxx}a write remote address (client) (xxx==remote) or connection peer address (xxx=peer)
%{xxx}i write value of incoming header with name xxx (escaped if required)
%{xxx}o write value of outgoing header with name xxx (escaped if required)
%{xxx}c write value of cookie(s) with name xxx (comma separated and escaped if required)
%{xxx}r write value of ServletRequest attribute with name xxx (escaped if required, value ?? if request is null)
%{xxx}s write value of HttpSession attribute with name xxx (escaped if required, value ?? if request is null)
%{xxx}p write local (server) port (xxx==local) or remote (client) port (xxx=remote)
%{xxx}t write timestamp at the end of the request formatted using the enhanced SimpleDateFormat pattern xxx
%{xxx}T write time taken to process the request using unit xxx where valid units are ms for milliseconds, us for microseconds, and s for seconds. %{s}T is equivalent to %T as well as %{us}T is equivalent to %D.

重点:

%D - Time taken to process the request in microseconds

%D 所记录的耗时单位是微秒,如何调整时间颗粒度,官方文档也指出了:

%{xxx}T write time taken to process the request using unit xxx where valid units are ms for milliseconds, us for microseconds, and s for seconds. %{s}T is equivalent to %T as well as %{us}T is equivalent to %D.
posted @ 2024-04-24 16:32  shea24  阅读(5)  评论(0编辑  收藏  举报