---
title: "Tomcat日志记录激活配置方法"
author: "ydsakyclguozi"
date: "2010-06-02T21:31:05Z"
tags: []
categories: ["企业架构"]
slug: "tomcat"
draft: false
lastmod: "2010-06-09T13:07:27Z"
id: 682412
url: "https://52347.iteye.com/blog/682412"
---

目录
1.	文档说明	1
2.	系统日志激活方式	1
2.1.	配置方式	1
2.2.	语法说明	1
2.3.	参数说明	2
2.4.	日志样例	2
3.	日志备份	2
4.	日志统计	3

1.	文档说明
	本文档描述Tomcat系统日志记录功能的激活和配置方式，适用于所有使用Tomcat4、5的Web应用。
2.	系统日志激活方式
2.1.	配置方式
编辑Tomcat/conf/server.xml文件,找到如下行:
<Context path="/cms" docBase="/calisportal/cms" debug="0"
reloadable="true" crossContext="true">
增加下面的行:
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="bbs_access_log." suffix=".txt"
pattern="%a -%U-%t-%m-%s-%S" resolveHosts="false"/>
2.2.	语法说明
directory="logs" ----存放日志的目录,请不要修改
prefix="bbs_access_log." ----日志文件名
suffix=".log" ---日志文件扩展名
pattern="%a...." ---日志字段参数
resolveHosts="false" ---
2.3.	参数说明
%a - Remote IP address(远程IP地址) 
%A - Local IP address (本地IP地址)
%b - Bytes sent, excluding HTTP headers, or ¡-¡ if zero(字节流出,排除HTTP头,如果是零字节,用¡-¡隔开) 
%B - Bytes sent, excluding HTTP headers(字节流出,排除HTTP头) 
%h - Remote host name (or IP address if resolveHosts 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 (返回HTTP状态代码)
%S - User session ID (获取用户会话ID)
%t - Date and time, in Common Log Format (记录提交时间)
%u - Remote user that was authenticated (if any), else ¡-¡ (识别远程用户在什么地方)
%U - Requested URL path (被请求的URL路径)
%v - Local server name (本地服务器名)

2.4.	日志样例

192.168.2.72-/index131312.jsp--GET-302-919E70792EC2B09D89A5137F62B523B6

192.168.2.215-/index131312.jsp--GET-302-C2ED835DF3103421D16C6E9A2B27E958

3.	日志备份
首先，找到所配置的系统日志指定目录，查看所有系统指定好的文件名为bbs_access_log.txt日志文件（日志文件的名字配置时自定义即可，例如prefix="bbs_access_log." suffix=".txt"），根据文件的修改时间确定当前正在使用的系统日志文件。
把目录中除当前使用的系统日志文件外的所有其他系统日志文件拷贝或者压缩到备份目录。这样可确保日志的备份不会重复，也不会遗失。
注意：因为系统日志文件会很大，而且系统访问量越大，日志文件大小的增长速度会越快，所以应及时备份，腾出磁盘空间，且压缩存储。