先看看 Tomcat 默认提供的访问日志格式,打开 %TOMCAT_HOME%/conf/server.xml 文件,找到 <Host> 标签,改标签下面的 <Valve> 标签就是用来配置访问日志。如下:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- 设置访问日志 --> <!-- className 设置访问日志实现类的全限定名称 --> <!-- directory 设置日志基础目录 --> <!-- prefix 设置日志文件前缀,默认为 localhost_access_log --> <!-- suffix 设置日志文件后缀,默认为 .txt --> <!-- pattern 设置日志格式 --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host>
启动 tomcat,访问 tomcat 页面,访问日志如下:
0:0:0:0:0:0:0:1 - - [29/May/2023:11:01:52 +0800] "GET / HTTP/1.1" 200 11452 0:0:0:0:0:0:0:1 - - [29/May/2023:11:01:52 +0800] "GET /tomcat.css HTTP/1.1" 200 5931 0:0:0:0:0:0:0:1 - - [29/May/2023:11:01:52 +0800] "GET /tomcat.png HTTP/1.1" 200 5103 0:0:0:0:0:0:0:1 - - [29/May/2023:11:01:52 +0800] "GET /asf-logo-wide.svg HTTP/1.1" 200 27530 0:0:0:0:0:0:0:1 - - [29/May/2023:11:01:52 +0800] "GET /bg-nav.png HTTP/1.1" 200 1401 0:0:0:0:0:0:0:1 - - [29/May/2023:11:01:52 +0800] "GET /bg-button.png HTTP/1.1" 200 713 0:0:0:0:0:0:0:1 - - [29/May/2023:11:01:52 +0800] "GET /bg-middle.png HTTP/1.1" 200 1918 0:0:0:0:0:0:0:1 - - [29/May/2023:11:01:52 +0800] "GET /bg-upper.png HTTP/1.1" 200 3103 0:0:0:0:0:0:0:1 - - [29/May/2023:11:01:53 +0800] "GET /favicon.ico HTTP/1.1" 200 21630 0:0:0:0:0:0:0:1 - - [29/May/2023:11:01:59 +0800] "GET /host-manager/html HTTP/1.1" 401 2098 0:0:0:0:0:0:0:1 - tomcat [29/May/2023:11:02:05 +0800] "GET /host-manager/html HTTP/1.1" 200 7907 0:0:0:0:0:0:0:1 - - [29/May/2023:11:02:05 +0800] "GET /host-manager/images/asf-logo.svg HTTP/1.1" 200 20712 0:0:0:0:0:0:0:1 - - [29/May/2023:11:02:05 +0800] "GET /host-manager/images/tomcat.gif HTTP/1.1" 200 2066
tomcat 访问日志格式可取值如下:
%a 这是记录访问者的IP,如果用了nginx 需要修改才可以看到,参考另一篇文章
%A 这是记录本地服务器的IP
%b 发送信息的字节数,不包括http头,如果字节数为0的话,显示为-
%B 发送信息的字节数,不包括http头。
%h 远端主机名(如果resolveHost=false,远端的IP地址)
%H 访问者的协议,这里是HTTP/1.0
%I (大写的i)当前请求的线程名称
%l (小写的L)官方解释:Remote logical username from identd (可能这样翻译:记录浏览者进行身份验证时提供的名字)(always returns '-')
%m 访问的方式,是GET还是POST
%p 本地接收访问的端口
%q 比如你访问的是aaa.jsp?bbb=ccc,那么这里就显示?bbb=ccc,就是querystring的意思
%r First line of the request (method and request URI) 请求的方法和URL
%s http的响应状态码
%S 用户的session ID,每个会话的值是相同的,关闭浏览器再打开,就会变化
%t 请求时间 月/日/年:时:分:秒 +0800 格式
%u 得到了验证的访问者,否则就是"-"
%U 访问的URL地址,我这里是/rightmainima/leftbott4.swf
%v 服务器名称,可能就是你url里面写的那个吧,我这里是localhost
%D Time taken to process the request,in millis,请求消耗的时间,以毫秒记
%T Time taken to process the request,in seconds,请求消耗的时间,以秒记
修改访问日志格式,例如:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %H %I %u %t "%r" %s %b %D %T" />
删除历史访问日志,便于查看。重启 tomcat,访问页面,查看访问日志如下:
0:0:0:0:0:0:0:1 - HTTP/1.1 http-nio-8080-exec-1 tomcat [29/May/2023:11:07:51 +0800] "GET /host-manager/html HTTP/1.1" 200 7775 144 0.144 0:0:0:0:0:0:0:1 - HTTP/1.1 http-nio-8080-exec-4 - [29/May/2023:11:08:24 +0800] "GET / HTTP/1.1" 200 11452 181 0.181 0:0:0:0:0:0:0:1 - HTTP/1.1 http-nio-8080-exec-5 - [29/May/2023:11:08:28 +0800] "GET /manager/status HTTP/1.1" 401 2536 926 0.926 0:0:0:0:0:0:0:1 - HTTP/1.1 http-nio-8080-exec-6 tomcat [29/May/2023:11:08:32 +0800] "GET /manager/status HTTP/1.1" 200 6991 29 0.029 0:0:0:0:0:0:0:1 - HTTP/1.1 http-nio-8080-exec-7 - [29/May/2023:11:08:33 +0800] "GET /manager/images/tomcat.gif HTTP/1.1" 200 2066 20 0.020 0:0:0:0:0:0:0:1 - HTTP/1.1 http-nio-8080-exec-8 - [29/May/2023:11:08:33 +0800] "GET /manager/images/asf-logo.svg HTTP/1.1" 200 20712 17 0.017 0:0:0:0:0:0:0:1 - HTTP/1.1 http-nio-8080-exec-1 tomcat [29/May/2023:11:10:15 +0800] "GET /host-manager/html HTTP/1.1" 200 7775 8 0.008