-
server.tomcat.accesslog.prefix = access
server.tomcat.accesslog.suffix = .log
server.tomcat.accesslog.file-date-format = -yyyyMMddHH有的时候我们要使用日志分析工具对日志进行分析,需要对日志进行格式化,比如,要把accessLog格式化成这样的格式
c-ip s-ip x-InstancePort date time-taken x-Protocol cs-method cs-uri sc-status x-ResponseSize bytes x-Referrer x-UserAgent
在tomcat的conf/server.xml加入配置:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%{X-Real-IP}i %A %p %{yyyy-MM-dd HH:mm:ss}t %D %H %m %U %s %B %B %{Referer}i %{User-Agent}i" />
关于accessLog的官方说明,请看 http://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Access_Logging
常用配置如下:
- %a - Remote IP address --远程IP地址
- %A - Local IP address --本地IP地址
- %b - Bytes sent, excluding HTTP headers, or '-' if zero --发送的字节数(Bytes sent), 不包括HTTP headers的字节,如果为0则展示'-'
- %B - Bytes sent, excluding HTTP headers --发送的字节数(Bytes sent), 不包括HTTP headers的字节
- %h - Remote host name (or IP address if
enableLookups
for the connector is false) --远程主机名称(如果resolveHosts为false则展示IP) - %H - Request protocol --请求协议
- %l - Remote logical username from identd (always returns '-') --远程用户名,始终为'-'(Remote logical username from identd)
- %m - Request method (GET, POST, etc.) --请求的方法(GET, POST等)
- %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) --请求的第一行(包括请求方法和请求的URI)
- %s - HTTP status code of the response --response的HTTP状态码(200,404等)
- %S - User session ID --用户的session ID
- %t - Date and time, in Common Log Format --日期和时间,Common Log Format格式
- %u - Remote user that was authenticated (if any), else '-' --被认证的远程用户, 不存在则展示'-'
- %U - Requested URL path --请求URL路径
- %v - Local server name --本地服务名
- %D - Time taken to process the request, in millis --处理请求的时间,单位为毫秒
- %T - Time taken to process the request, in seconds --处理请求的时间,单位为秒
- %F - Time taken to commit the response, in millis --提交响应的时间,以毫秒为单位
- %I - Current request thread name (can compare later with stacktraces) --当前请求的线程名
Access Log中也支持cookie,请求header,响应headers,Session或者其他在ServletRequest中的对象的信息
%{xxx}i
write value of incoming header with namexxx
%{xxx}o
write value of outgoing header with namexxx
%{xxx}c
write value of cookie with namexxx
%{xxx}r
write value of ServletRequest attribute with namexxx
%{xxx}s
write value of HttpSession attribute with namexxx
%{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 patternxxx
如果tomcat前面有nginx做负载均衡,则要修改nginx的配置
Nginx的server主机配置段中添加:
proxy_set_header X-Real-IP $remote_addr; #真实的远端IP地址
so 再tomcat accessLog用%{X-Real-IP}i 获取即可