自訂Tomcat的localhost_access_log

自訂Tomcat的localhost_access_log

預設格式

Tomcat預設的localhost_access_log格式為

1
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>

可自定義欄位

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
%a - Remote IP address
%A - Local IP address
%b - Bytes sent, excluding HTTP headers, or '-' if zero
%B - Bytes sent, excluding HTTP headers
%h - Remote host name (or IP address if resolveHosts is false)
%H - Request protocol
%l - Remote logical username from identd (always returns '-')
%m - Request method (GET, POST, etc.)
%p - Local port on which this request was received
%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
%u - Remote user that was authenticated (if any), else '-'
%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)

若要修改localhost_access_log格式,需修改/usr/local/tomcat/conf/server.xml檔案如下

範例

1
2
3
4
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%S %h %l %u %t &quot;%r&quot; %s %b" />

Output:
- 10.10.100.1 - - [13/Mar/2020:15:33:37 +0800] "POST /api/Servlet HTTP/1.1" 200 23