virgo-tomcat访问日志的详细配置

Tomcat 日志信息分为两类:
1、运行中的日志,它主要记录运行的一些信息,尤其是一些异常错误日志信息。
2、访问日志信息,它记录的访问的时间、IP、访问的资料等相关信息。

关于tomcat访问日志的产生样式说明如下(从官方文档中摘录):

%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 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
%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
%F - Time taken to commit the response, in millis
%I - Current request thread name (can compare later with stacktraces)

There is also support to write information incoming or outgoing headers, cookies, session or request attributes and special timestamp formats. 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

上面最后一段的内容大概意思是我们还可以将cookie, 客户端请求中带的HTTP头(incoming header), 会话(session)或是ServletRequest中的数据都写到Tomcat的访问日志中

我们可以用下面的语法来引用。

%{xxx}i      –      记录客户端请求中带的HTTP头xxx(incoming headers) 
%{xxx}c      –      记录特定的cookie xxx 
%{xxx}r      –      记录ServletRequest中的xxx属性(attribute) 
%{xxx}s      –      记录HttpSession中的xxx属性(attribute)

对于日志项值还可以可以为:common与combined,这两个预先设置好的格式对应的日志输出内容如下:
common   的值: %h %l %u %t %r %s %b
combined 的值: %h %l %u %t %r %s %b %{Referer}i %{User-Agent}i

但本人建议采用以下具体的配置,因为标准配置有一些重要的日志数据无法生。

%{Host}i                 是用于发送的 HTTP请求中的 Host这个字段的值。
%h                           访问的用户主机名(if resolveHosts is false,就显示IP地址)
%B                           访问资源返回的流量,单位为Byte,即字节
%T                           访问所使用的时间,同一资源、每次访问的时间不一定相同。
%t                            时间字段,默认的时间格式为[13/Jun/2014:13:41:52 +0800]
%s                            访问返回的http状态码
%r                            访问的方式(post或者是get),访问的URI和使用的http协议版本(返回3列值)
%{User-Agent}i       用户的User-Agent信息

如本人使用的是virgo-tomcat-server(集成版本的tomcat)服务器,我们可以修改virgo-tomcat-server-3.6.0.RELEASE/configuration/tomcat-server.xml配置文件,配置pattern项的值如下图所示:

对于%t我们可以使用%{yyyy-MM-dd HH:mm:ss}t作格化时间显示调整。可方便我们更好的阅读、个人建议可以将格式化的值用中括号[]括起来。

有了这些数据,我们可以根据时间段做以下的分析处理(图片使用jfreechart工具动态生成):
* 访问请求数、独立IP数统计
* 访问资料文件数统计以及访问流量统计
* 访问处理响应时间统计
* 统计所有404与500错误页面
* 统计访问最频繁页面
* 统计访问处理时间最久页面
* 统计并发访问频率最高的页面

原文地址:https://www.cnblogs.com/5201351/p/4195446.html