日志
- 参考文档:https://cwiki.apache.org/confluence/display/TOMCAT/Logging
主配置文件
/usr/local/tomcat/conf/logging.properties日志记录格式说明
- 参考文档:https://tomcat.apache.org/tomcat-9.0-doc/config/valve.html#Access_Logging
%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
%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 (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
%u - Remote user that was authenticated (if any), else '-' (escaped if required)
%U - Requested URL path
%v - Local server name
%D - Time taken to process the request in millis. Note: Inhttpd %D is microseconds. Behaviour will be aligned to httpdin Tomcat 10 onwards.
%T - Time taken to process the request, in seconds. Note: Thisvalue has millisecond resolution whereas in httpd it hassecond resolution. Behaviour will be align to httpdin Tomcat 10 onwards.
%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.
#There is also support to write information incoming or outgoingheaders, cookies, session or request attributes and specialtimestamp formats.It is modeled after the Apache HTTP Server log configurationsyntax. Each of them can be used multiple times with different xxx keys:
%{xxx}a write remote address (client) (xxx==remote) orconnection 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 with name xxx (escaped if required)
%{xxx}r write value of ServletRequest attribute with name xxx (escaped if required)
%{xxx}s write value of HttpSession attribute with name xxx (escaped if required)
%{xxx}p write local (server) port (xxx==local) orremote (client) port (xxx=remote)
%{xxx}t write timestamp at the end of the request formatted using theenhanced SimpleDateFormat pattern xxx
...时间格式说明
| 格式符 | 说明 |
|---|---|
| %a | 星期的英文单词的缩写:如星期一, 则返回 Mon |
| %A | 星期的英文单词的全拼:如星期一,返回 Monday |
| %b | 月份的英文单词的缩写:如一月, 则返回 Jan |
| %B | 月份的引文单词的缩写:如一月, 则返回 January |
| %c | 返回datetime的字符串表示,如03/08/15 23:01:26 |
| %d | 返回的是当前时间是当前月的第几天 |
| %f | 微秒的表示: 范围: [0,999999] |
| %H | 以24小时制表示当前小时 |
| %I | 以12小时制表示当前小时 |
| %j | 返回 当天是当年的第几天 范围[001,366] |
| %M | 返回月份 范围[0,12] |
| %m | 返回分钟数 范围 [0,59] |
| %P | 返回是上午还是下午–AM or PM |
| %s | 返回秒数 范围 [0,61]。。。手册说明的 |
| %U | 返回当周是当年的第几周 以周日为第一天 |
| %W | 返回当周是当年的第几周 以周一为第一天 |
| %w | 当天在当周的天数,范围为[0, 6],6表示星期天 |
| %x | 日期的字符串表示 :03/08/15 |
| %X | 时间的字符串表示 :23:22:08 |
| %y | 两个数字表示的年份 15 |
| %Y | 四个数字表示的年份 2015 |
| %z | 与utc时间的间隔 (如果是本地时间,返回空字符串) |
| %Z | 时区名称(如果是本地时间,返回空字符串) |
范例:记录格式改为json
#默认记录格式
#vim /apps/tomcat/conf/server.xml
...
pattern="%h %l %u %{Y-M-H:m:s}t "%r" %s %b"
...
#输出格式如下:
10.0.0.102 - - 2021-10-15:36:7 "GET / HTTP/1.1" 200 11156
--------------------------------------------------------------------------------------
#转为ELK需要的json格式,说明:" 在html中表示双引号
vim /apps/tomcat/conf/server.xml
...
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="tomcat_access_log" suffix=".log"
pattern="{"client":"%h", "client user":"%l", "authenticated":"%u", "access time":"%t", "method":"%r", "status":"%s", "send bytes":"%b", "Query?string":"%q", "partner":"%{Referer}i", "Agent version":"%{User-Agent}i"}"/>
#以上配置文件在135行
...
--------------------------------------------------------------------------------------
#输出格式如下:
{"client":"10.0.0.102", "client user":"-", "authenticated":"-", "access time":"[15/Oct/2021:15:42:25 +0800]", "method":"GET / HTTP/1.1", "status":"200", "send bytes":"11156", "Query?string":"", "partner":"-", "Agent version":"curl/7.68.0"}
--------------------------------------------------------------------------------------
#json转换器显示结果:
{
"client":"10.0.0.102",
"client user":"-",
"authenticated":"-",
"access time":"[15/Oct/2021:15:42:25 +0800]",
"method":"GET / HTTP/1.1",
"status":"200",
"send bytes":"11156",
"Query?string":"",
"partner":"-",
"Agent version":"curl/7.68.0"
}