下面着重讲下pattern。它的参数比较多。可以设置成common,combined两种格式。
-
common的值:%h %l %u %t %r %s %b
combined的值:%h %l %u %t %r %s %b %{Referer}i %{User-Agent}i (至于combined的值的最后两个为什么会这样,我也不太清楚)
%a 这是记录访问者的IP,在日志里是127.0.0.1 %A 这是记录本地服务器的IP,在日志里是192.168.254.108 %b 发送信息的字节数,不包括http头,如果字节数为0的话,显示为- %B 发送信息的字节数,不包括http头。 %h 服务器的名称。如果resolveHosts为false的话,这里就是IP地址了,例如我的日志里是10.217.14.16 %H 访问者的协议,这里是HTTP/1.0 %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,这个session ID大家可以另外查一下详细的解释,反正每次都会生成不同的session ID %t 请求时间 %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,请求消耗的时间,以秒记
附:参考官方文档: http://tomcat.apache.org/tomcat-5.5-doc/config/valve.html
二、配置打印POST参数
另外%r参数能打印出请求的url和get参数。如果url指定访问方式是post,post的参数是打印不出来的。当需要打印post参数,该怎么办?
大家注意到我开篇举例Valve配置里的%{postdata}r。没错,这个combined格式的patterrn可以实现。但是只在valve里配置这个东东还不够。因为postdata使我们自定义的参数名。需要在request中设置这个值。下面附上设置postdata到request中的代码。
package com.xiaoxiliu
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class PostDataDumperFilter implements Filter {
Logger logger = LoggerFactory.getLogger(getClass());
private FilterConfig filterConfig = null;
public void destroy() {
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
if (filterConfig == null)
return;
Enumeration<String> names = request.getParameterNames();
StringBuilder output = new StringBuilder();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
output.append(name).append("=");
String values[] = request.getParameterValues(name);
for (int i = 0; i < values.length; i++) {
if (i > 0) {
output.append("' ");
}
output.append(values[i]);
}
if (names.hasMoreElements())
output.append("&");
}
request.setAttribute("postdata", output);
logger.debug("postdata: " + output);
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
}









