博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet、Tomcat访问(access)日志配置、记录Post请求参数
阅读量:6902 次
发布时间:2019-06-27

本文共 4329 字,大约阅读时间需要 14 分钟。

一、运行环境:

Maven: ,

Tomcat: ,

JDK: ,

MavenDependency:

javax.servlet.jsp
javax.servlet.jsp-api
3.1.0
provided

 

二、配置与说明

tomcat访问日志格式配置,在config/server.xml里Host标签下加上:

 

我们在日志文件中将看到如下文本:

 

来访ip  |  session ID  |  请求时间  |  请求的方法和URL  |  POST参数  |  http的响应状态码  |  来源网页  |  浏览器类型  |  请求消耗的秒数  |  发送信息的字节数

182.92.182.165 5ABD1AC3DF321E752B0DE133A81FFECA [2018-04-09 00:01:23587] "GET /api/notify.do?code=6219&LinkID=42447 HTTP/1.1" [mobile=26219&LinkID=42447] 200 - [Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)] 1.83 6

 

参数说明:

className        官方文档上说了:This MUST be set to org.apache.catalina.valves.AccessLogValve to use the default access log valve。
directory 日志文件存放的目录。通常设置为tomcat下已有的那个logs文件。
prefix 日志文件的名称前缀。
suffix 日志文件的名称后缀。
pattern 最主要的参数。下面会细讲。
resolveHosts 如果是true,tomcat会将这个服务器IP地址通过DNS转换为主机名;如果是false,就直接写服务器IP地址啦。默认false。
rotatable  默认为true,tomcat生成的文件名为prefix(前缀)+.+时间(一般是按天算)+.+suffix(后缀),如:localhost_access_log.2007-09-22.txt。设置为false的话,tomcat会忽略时间,不会生成新文件,文件名就是:localhost_access_log.txt。长此以往,这个日志文件会超级大
condition 这个参数不太实用,可设置任何值,比如设置成condition="tkq",那么只有当ServletRequest.getAttribute("tkq")为空的时候,该条日志才会被记录下来。
fileDateFormat                                                                                 
顾名思义,就是时间格式嘛。但这个时间格式是针对日志文件名起作用的。咱们生成的日志文件全名:localhost_access_log.2016-09-22.txt,这里面的2016-09-22就是这么来的。如果想让tomcat每小时生成一个日志文件,也很简单,将这个值设置为:fileDateFormat="yyyy-MM-dd.HH",当然也可以按分钟生成什么的,自己改改吧^_^

 

下面着重讲下pattern。它的参数比较多。可以设置成common,combined两种格式。

common的值:%h %l %u %t %r %s %b

combined的值:%h %l %u %t %r %s %b %{Referer}i %{User-Agent}i

 

%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,请求消耗的时间,以秒记

 

附:参考官方文档: https://tomcat.apache.org/tomcat-8.5-doc/config/valve.html#Access_Logging

 

三、配置打印POST参数

另外%r参数能打印出请求的url和get参数。如果url指定访问方式是post,post的参数是打印不出来的。当需要打印post参数,该怎么办?

大家注意到开篇举例Valve配置里的%{postData}r。没错,这个combined格式的patterrn可以实现。

但是只在valve里配置是还不够的。因为postData是自定义的参数名。需要在request中设置这个值。

下面附上设置postData到request中的代码:

import javax.servlet.*;import java.io.IOException;import java.util.Enumeration;/** * 该过滤器是为了给tomcat打印access_log日志时,打印POST参数用 */public class PostDataDumperFilter implements Filter {    private FilterConfig filterConfig = null;    @Override    public void init(FilterConfig filterConfig) throws ServletException {        this.filterConfig = filterConfig;    }    @Override    public void destroy() {        this.filterConfig = null;    }    @Override    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)            throws IOException, ServletException {        if (filterConfig == null)            return;        Enumeration
names = request.getParameterNames(); StringBuilder output = new StringBuilder(); while (names.hasMoreElements()) { String name = 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); chain.doFilter(request, response); }}

 

在 项目路径/src/main/webapp/WEB-INF/web.xml 中添加配置该 filter :

post-data-dumper-filter
com.xxxx.jstl.filter.PostDataDumperFilter
post-data-dumper-filter
/*

 

至此,配置完毕!

 

 

PS:

http://blog.csdn.net/qq_30121245/article/details/52861935

 

你可能感兴趣的文章
解决数据重复导致查询出错问题
查看>>
springmvc(4)注解简单了解
查看>>
HTMLCSS学习笔记(四)----浮动原理及清浮动
查看>>
探究如何求两数的最大公约数(两种方法)
查看>>
Better Django models
查看>>
第 8 章 容器网络 - 054 - 准备 macvlan 环境
查看>>
第 10 章 容器监控 - 081 - Weave Scope 多主机监控
查看>>
FLASK爬坑笔记
查看>>
[haoi2008]圆上的整点
查看>>
node.js常用方法
查看>>
iscroll手册
查看>>
5.Struts2框架中的ServletAPI如何获取
查看>>
java学习路线
查看>>
layui-学习01-基本api
查看>>
Angular 表单验证 基础篇
查看>>
大话设计模式第十三章---建造者模式
查看>>
Google Chrome浏览器的使用方法
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource
查看>>
『左偏树 Leftist Tree』
查看>>
打印菱形
查看>>