Tomcat的访问日志是靠org.apache.catalina.valves.AccessLogValve来控制的,你可以修改$tomcat/conf/server.xml来启用它 ($tomcat是Tomcat安装的目录)。AccessLogValve默认应该是注释掉的,简单的将其注释去掉,然后重启Tomcat就可以了。$D.j.N5c9y
zjq_b0
以下是Tomcat默认的配置:51Testing软件测试网9N:fU*v7W#ty
className=“org.apache.catalina.valves.AccessLogValve”
[2QV8c8I2g-L2z0directory=“logs” prefix=“localhost_access_log.” suffix=“.txt”51Testing软件测试网GZo4s}
hK
pattern=“common” resolveHosts=“false”/>\Y%M-nL+x0
你可以设置日志保存的目录(directory),日志的文件名的前缀(prefix),后缀(suffix)和日志的具体格式。保存目录,文件名的前缀、后缀都很简单,一般默认设置也就可以了。resolveHost出于性能的考虑,一般也设为false. 但访问日志的格式(pattern)却有很多的选项供你选择。以下列出了一些基本的日志格式项:51Testing软件测试网(w7f*w)QQ(G0`\;J&~nR#E2j&Tr0%a – 远程主机的IP (Remote IP address)9o~2xO6g0
51Testing软件测试网$ke| Sh%ks
%A – 本机IP (Local IP address)51Testing软件测试网1z3r+_5~J?J^
51Testing软件测试网nY7PeL_I
%b – 发送字节数,不包含HTTP头,0字节则显示 ‘-’ (Bytes sent, excluding HTTP headers, or ‘-’ if no bytes51Testing软件测试网
Fh.S7[p1_hR
were sent)x&T^5Dm:KOx.e#_.~0
^YGm!`d3A0%B – 发送字节数,不包含HTTP头 (Bytes sent, excluding HTTP headers)/P'DWMtcy7W
L\0
Dab1fU0%h – 远程主机名 (Remote host name)51Testing软件测试网/fwh:iZhJ
v~'f3S&B@#I0%H – 请求的具体协议,HTTP/1.0 或 HTTP/1.1 (Request protocol)51Testing软件测试网dB4Cw%\o0IY
51Testing软件测试网}9_qd2p FRuZ
%l – 远程用户名,始终为 ‘-’ (Remote logical username from identd (always returns ‘-’))51Testing软件测试网)wo(@J:A)p0r+[
b2D
fP SP0iX0%m – 请求方式,GET, POST, PUT (Request method)51Testing软件测试网t0p
CwEJi5i
51Testing软件测试网Lc7L&a'DE*Z6uoUK
%p – 本机端口 (Local port)X(_1_.mmkm$@Oei0
51Testing软件测试网]C_t'[,OW
%q – 查询串 (Query string (prepended with a ‘?’ if it exists, otherwise
HM3TnE*u8_0an empty string)a)F][ ~N,R0
51Testing软件测试网Vi!J7DIXk,fq7o\T
%r – HTTP请求中的第一行 (First line of the request)51Testing软件测试网*bl*So&JS B
5KGJ0n8\9q0%s – HTTP状态码 (HTTP status code of the response)51Testing软件测试网Of}{0@
yC`'C?G8SbW0%S – 用户会话ID (User session ID)51Testing软件测试网6O+P\4l0{'o\8Ng,h!~F
51Testing软件测试网n!\ _-i`,mA~
%t – 访问日期和时间 (Date and time, in Common Log Format format)?&h%\zU0
51Testing软件测试网 ui*`#lq,qr8Z_C't
%u – 已经验证的远程用户 (Remote user that was authenticated51Testing软件测试网&u*J5_] W4O
51Testing软件测试网1e1{,p1K:@$V
%U – 请求的URL路径 (Requested URL path)Z}cCe0
`DX-`%i5ONs0%v – 本地服务器名 (Local server name)51Testing软件测试网gVU#n
[0p
,e-D#BF1r!^M/w o6B"E;j*K0%D – 处理请求所耗费的毫秒数 (Time taken to process the request, in millis)#A,S]~'D+w.Z$U0
51Testing软件测试网2q7n#Z9v#L
%T – 处理请求所耗费的秒数 (Time taken to process the request, in seconds)51Testing软件测试网#{m-Vc(o#D7eT"y
你可以用以上的任意组合来定制你的访问日志格式,也可以用下面两个别名common和combined来指定常用的日志格式:51Testing软件测试网)C}u!p}rl51Testing软件测试网3W8BQpZ8`z u
common – %h %l %u %t "%r" %s %b51Testing软件测试网qZ9Ti;Qncy
51Testing软件测试网O+q9z5o(x,y1c
combined -
"Q_ FETm0%h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"51Testing软件测试网v3\eQGR
另外你还可以将cookie, 客户端请求中带的HTTP头(incoming header), 会话(session)或是ServletRequest中的数据都写到Tomcat的访问日志中,你可以用下面的语法来引用。hR8DyeD0;w6fz`4f0%{xxx}i – 记录客户端请求中带的HTTP头xxx(incoming headers)51Testing软件测试网`4jZ,Zno-M
51Testing软件测试网,ps)|)M*FO
%{xxx}c – 记录特定的cookie xxx&c2ju*pO'q2_;x0
PMb4VQaN0%{xxx}r – 记录ServletRequest中的xxx属性(attribute)]m%UJ;Hj3?'V0
51Testing软件测试网~!{[i&T:Nvc ?;K
%{xxx}s – 记录HttpSession中的xxx属性(attribute)51Testing软件测试网}6SHd3nS(R
{;a)D
比如下面是实际的一个访问日志格式的配置:51Testing软件测试网^+aN-{)auj
className=“org.apache.catalina.valves.AccessLogValve”
S-e#pC5m4o:K(e2L/]0directory=“logs” prefix=“phone_access_log.” suffix=“.txt”51Testing软件测试网l(s'^:K5A
pattern=“%h %l %T %t %r %s %b %{Referer}i %{User-Agent}i MSISDN=%{x-up-calling-line-id}i”resolveHosts=“false”/>}e*KC$a)}.vHu#]0
其中日志格式(pattern)指定为”%h %l %T %t %r %s %b %{Referer}i %{User-Agent}i MSISDN=%{x-up-calling-line-id}i“,则实际的访问日志中将会包括:*q!mM7@@!OFo)}7N0z_)m4m(ZtT7h6P0%h – 远程主机名51Testing软件测试网KV'I[+A
6c6{+A*_AOa`0%l - 远程用户名,始终为 ‘-’51Testing软件测试网+?5E,Z*e6|S*vA
7O0~1q+W7^hq;Z4|R0%T - 处理请求所耗费的秒数3yNby"s4E0
7GIb u!lP$}`0%t – 访问日期和时间:oLH8gTD.k%[)s+_N0
51Testing软件测试网1Z6P^g{&?6j
%r – HTTP请求中的第一行51Testing软件测试网nu8eZ7`
51Testing软件测试网W'Z~7Q2f W
%s – HTTP状态码51Testing软件测试网8Y|ZWGP$Z;m%]4G
51Testing软件测试网z)G]4m$`uNWR
%b – 发送字节数,不包含HTTP头(0字节则显示 ‘-’)51Testing软件测试网%f[z)exQ#h
51Testing软件测试网]ru8K^|;gX,j
%{Referer}i – Referer URL51Testing软件测试网LB'tC)]Y*~Ua8`
I(H'O,]r"K2pg0%{User-Agent}i – User agent$z^){cQ-|;e'D_0
rrwpC0MSISDN=%{x-up-calling-line-id}i – 手机号51Testing软件测试网:C dy,qbn
实际的访问日志如下:6u4_R*wK s ?0
xxx.xxx.xx.xxx – 0.270 [14/Jul/2008:13:10:53 +0800] POST /phone/xxx/gprs HTTP/1.1 200 91812 – SonyEricssonW890i/R1EA Profile/MIDP-2.1 Configuration/CLDC-1.1 MSISDN=11111111111}sqFQ2S1f0
… …O4I(p0lYh%Mi0
xxx.xxx.xx.xxx – 0.083 [14/Jul/2008:21:20:55 +0800] POST /phone/xxx/gprs HTTP/1.1 200 404 – SonyEricssonW910i/R1FA Profile/MIDP-2.1 Configuration/CLDC-1.1 MSISDN=1111111111151Testing软件测试网
W
c,ZZ1k A5\J
x
Y[kc&Q(M#r0
$wJ$d@3S'w(^.Vf051Testing软件测试网"b#jCLe2Q