新版tomcat accesslog %D 打印的耗时为微妙,不是毫秒,那如果要打印毫秒怎么办呢,查询tomcat源码发现
org.apache.catalina.valves.AbstractAccessLogValve类定义了相关行为,核心代码如下:
protected AccessLogElement createAccessLogElement(String name, char pattern) {
switch (pattern) {
case 'i':
return new HeaderElement(name);
case 'c':
return new CookieElement(name);
case 'o':
return new ResponseHeaderElement(name);
case 'a':
return new RemoteAddrElement(name);
case 'p':
return new PortElement(name);
case 'r':
if (TLSUtil.isTLSRequestAttribute(name)) {
tlsAttributeRequired = true;
}
return new RequestAttributeElement(name);
case 's':
return new SessionAttributeElement(name);
case 't':
return new DateAndTimeElement(name);
case 'T':
// ms for milliseconds, us for microseconds, and s for seconds
if ("ns".equals(name)) {
return new ElapsedTimeElement(ElapsedTimeElement.Style.NANOSECONDS);
} else if ("us".equals(name)) {
return new ElapsedTimeElement(ElapsedTimeElement.Style.MICROSECONDS);
} else if ("ms".equals(name)) {
return new ElapsedTimeElement(ElapsedTimeElement.Style.MILLISECONDS);
} else if ("fracsec".equals(name)) {
return new ElapsedTimeElement(ElapsedTimeElement.Style.SECONDS_FRACTIONAL);
} else {
return new ElapsedTimeElement(false, false);
}
default:
return new StringElement("???");
}
}
可以看到,如果要支持毫秒,那么使用 %T的形式格式化 %{ms}T
其中含义,源代码有注释:
bstract implementation of the Valve interface that generates a web server access log with the detailed line contents matching a configurable pattern. The syntax of the available patterns is similar to that supported by the Apache HTTP Server mod_log_config module.
Patterns for the logged message may include constant text or any of the following replacement strings, for which the corresponding information from the specified Response is substituted:
%a - Remote IP address
%A - Local IP address
%b - Bytes sent, excluding HTTP headers, or '-' if no bytes were sent
%B - Bytes sent, excluding 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 microseconds
%T - Time taken to process the request, in seconds
%F - Time taken to commit the response, in milliseconds
%I - current Request thread name (can compare later with stacktraces)
%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.
In addition, the caller can specify one of the following aliases for commonly utilized patterns:
common - %h %l %u %t "%r" %s %b
combined - %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"
There is also support to write information from the cookie, incoming header, the Session or something else in the ServletRequest. It is modeled after the Apache HTTP Server log configuration syntax:
%{xxx}i for incoming headers
%{xxx}o for outgoing response headers
%{xxx}c for a specific cookie
%{xxx}r xxx is an attribute in the ServletRequest
%{xxx}s xxx is an attribute in the HttpSession
%{xxx}t xxx is an enhanced SimpleDateFormat pattern (see Configuration Reference document for details on supported time patterns)
%{xxx}T xxx is the unit for the time taken to process the request (see Configuration Reference document for details on supported units)
Conditional logging is also supported. This can be done with the conditionUnless and conditionIf properties. If the value returned from ServletRequest. getAttribute(conditionUnless) yields a non-null value, the logging will be skipped. If the value returned from ServletRequest. getAttribute(conditionIf) yields the null value, the logging will be skipped. The condition attribute is synonym for conditionUnless and is provided for backwards compatibility.
For extended attributes coming from a getAttribute() call, it is you responsibility to ensure there are no newline or control characters.
application.yml 配置文件如下:
server:
port: 8081
tomcat:
accesslog:
enabled: true
directory: c://logs
max-days: 5
pattern: '%{yyyy-MM-dd HH:mm:ss}t %a %A %m %U%q %H %s %{ms}Tms %b %I %{User-Agent}i'
spring:
application:
name: demo
最终效果如下:
2024-12-06 16:44:35 0:0:0:0:0:0:0:1 172.28.128.1 GET /test/test?sss=21 HTTP/1.1 200 5ms 12 http-nio-8081-exec-3 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
2024-12-06 16:44:35 0:0:0:0:0:0:0:1 172.28.128.1 GET /test/test?sss=21 HTTP/1.1 200 1ms 12 http-nio-8081-exec-4 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
2024-12-06 16:44:35 0:0:0:0:0:0:0:1 172.28.128.1 GET /test/test?sss=21 HTTP/1.1 200 4ms 12 http-nio-8081-exec-7 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36