Tomcat访问日志浅析

Tomcat的访问日志是靠org.apache.catalina.valves.AccessLogValve来控制的,你可以修改$tomcat/conf/server.xml来启用它 ($tomcat是Tomcat安装的目录)。AccessLogValve默认应该是注释掉的,简单的将其注释去掉,然后重启Tomcat就可以了。

以下是Tomcat默认的配置:

<Valve className=“org.apache.catalina.valves.AccessLogValve”
directory=“logs” prefix=“localhost_access_log.” suffix=“.txt”
pattern=“common” resolveHosts=“false”/>

你可以设置日志保存的目录(directory),日志的文件名的前缀(prefix),后缀(suffix)和日志的具体格式。保存目录,文件名的前缀、后缀都很简单,一般默认设置也就可以了。resolveHost出于性能的考虑,一般也设为false. 但访问日志的格式(pattern)却有很多的选项供你选择。以下列出了一些基本的日志格式项:

  • %a – 远程主机的IP (Remote IP address)
  • %A – 本机IP (Local IP address)
  • %b – 发送字节数,不包含HTTP头,0字节则显示 ‘-’ (Bytes sent, excluding HTTP headers, or ‘-’ if no bytes
    were sent)
  • %B – 发送字节数,不包含HTTP头 (Bytes sent, excluding HTTP headers)
  • %h – 远程主机名 (Remote host name)
  • %H – 请求的具体协议,HTTP/1.0 或 HTTP/1.1 (Request protocol)
  • %l – 远程用户名,始终为 ‘-’ (Remote logical username from identd (always returns ‘-’))
  • %m – 请求方式,GET, POST, PUT (Request method)
  • %p – 本机端口 (Local port)
  • %q – 查询串 (Query string (prepended with a ‘?’ if it exists, otherwise
    an empty string)
  • %r – HTTP请求中的第一行 (First line of the request)
  • %s – HTTP状态码 (HTTP status code of the response)
  • %S – 用户会话ID (User session ID)
  • %t – 访问日期和时间 (Date and time, in Common Log Format format)
  • %u – 已经验证的远程用户 (Remote user that was authenticated
  • %U – 请求的URL路径 (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)

你可以用以上的任意组合来定制你的访问日志格式,也可以用下面两个别名common和combined来指定常用的日志格式:

  • common – %h %l %u %t "%r" %s %b
  • combined -
    %h %l %u %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"

另外你还可以将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)

比如下面是实际的一个访问日志格式的配置:

<Valve className=“org.apache.catalina.valves.AccessLogValve”
directory=“logs” prefix=“phone_access_log.” suffix=“.txt”
pattern=“%h %l %T %t %r %s %b %{Referer}i %{User-Agent}i MSISDN=%{x-up-calling-line-id}i”resolveHosts=“false”/>

其中日志格式(pattern)指定为”%h %l %T %t %r %s %b %{Referer}i %{User-Agent}i MSISDN=%{x-up-calling-line-id}i“,则实际的访问日志中将会包括:

  • %h – 远程主机名
  • %l - 远程用户名,始终为 ‘-’
  • %T - 处理请求所耗费的秒数
  • %t – 访问日期和时间
  • %r – HTTP请求中的第一行
  • %s – HTTP状态码
  • %b – 发送字节数,不包含HTTP头(0字节则显示 ‘-’)
  • %{Referer}i – Referer URL
  • %{User-Agent}i – User agent
  • MSISDN=%{x-up-calling-line-id}i – 手机号

实际的访问日志如下:

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

… …

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=11111111111

package test; import java.io.DataInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class TestGetGroup { @Test public void main() { // getfile("http://localhost/getportrait.do?Uri=tel:13811094505&Size=2400", // "F:\\testpicture\\11111.jpg"); // getTest("http://web.devtest.mypica.com:8082/getportrait.do?Uri=tel:13811094505&Size=96", // "F:\\testpicture\\11111.jpg"); getTest( "http://218.241.177.244:3080/GetGrouPortrait.do?group-uri=sip:3000558@fetion.com&Size=96", "F:\\testpicture\\ggggg.jpg"); // getTest( // "http://127.0.0.1:8080/GetGrouPortrait.do?group-uri=sip:3000558@fetion.com&Size=96", // "F:\\testpicture\\ccccc.jpg"); } @BeforeClass public static void setUpBeforeClass() throws Exception { } @AfterClass public static void tearDownAfterClass() throws Exception { } @After public void tearDown() throws Exception { } private static void getTest(String strURL, String saveFilePath) { // 构造HttpClient的实例 HttpClient httpClient = new HttpClient(); // 创建GET方法的实例 GetMethod getMethod = new GetMethod(strURL); // 使用系统提供的默认的恢复策略 getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); getMethod.setRequestHeader("X-Up-Calling-Line-ID", "13800138006"); getMethod.setRequestHeader("Accept", "image/png,image/jpeg,image/Gif"); try { // 执行getMethod int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } // //读取内容 // byte[] responseBody = getMethod.getResponseBody(); // //处理内容 // System.out.println(new String(responseBody)); InputStream stream = getMethod.getResponseBodyAsStream(); DataInputStream in = new DataInputStream(stream); File savedFile = new File(saveFilePath); RandomAccessFile oSavedFile = new RandomAccessFile(savedFile, "rw"); byte[] inc = new byte[1024]; int insize = 0; while ((insize = in.read(inc)) != -1) { oSavedFile.write(inc, 0, insize); } oSavedFile.close(); in.close(); } catch (HttpException e) { // 发生致命的异常,可能是协议不对或者返回的内容有问题 System.out.println("Please check your provided http address!"); e.printStackTrace(); } catch (IOException e) { // 发生网络异常 e.printStackTrace(); } finally { // 释放连接 getMethod.releaseConnection(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值