在配置文件application.yml里面来配置tomcat accesslog日志
在application.yml中添加如下配置:
server:
tomcat:
accesslog:
enabled: true // 是否打印accesslog日志。为true时为打印(默认是false)
buffered: false // 用于确定是否将缓冲日志记录的标志。如果设置为false,则将在每个请求之后写入访问日志。默认值:true
directory: /data0/www/logs/xxxx.xx.com // 日志文件存放的路径
prefix: access_log // 日志名称前缀
suffix: .log // 日志名称后缀
file-date-format: -yyyy-MM-dd // 日志文件名中的日期格式。以天为单位
pattern: '%{X-Real-IP}i - %{Host}i %t "%r" %{Content-Length}i %s %b %D "%{Referer}i" "%{User-Agent}i" %a "%I" "-"' // 见下
rotate: true // 是否启用日志轮转。默认为true(这个参数决定是否需要切换切换日志文件,如果被设置为false,则日志文件不会切换,即所有文件打到同一个日志文件中,
// 并且file-date-format参数也会被忽略)
日志记录格式(pattern),形式参照 :
nginx 日志 log_format:
log_format main '$http_x_real_ip - $http_host [$time_local] "$request" $request_length '
'$status $body_bytes_sent $request_time "$http_referer" '
'"$http_user_agent" $remote_addr "-" "-" ';
tomcat access log pattern:
pattern: '%{X-Real-IP}i - %{Host}i %t "%r" %{Content-Length}i'
'%s %b %D "%{Referer}i"'
'"%{User-Agent}i" %a "%I" "-"'
%{X-Real-IP}i: http X-Real_IP
%{Host}i: http Host
%t: Date and time, in Common Log Format
%r: First line of the request (method and request URI)
%{Content-Length}i: request length
%s: HTTP status code of the response
%b: Bytes sent, excluding HTTP headers, or ‘-’ if zero
%D: Time taken to process the request, in millis
%{Referer}i: http Referer
%{User-Agent}i: http User-Agent
%a: Remote IP address
%I: Current request thread name (can compare later with stacktraces)
参考官方文档:
https://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Access_Log_Valve
http://xiaobaoqiu.github.io/blog/2014/12/30/tomcat-access-logpei-zhi/