tomcat的acess_log打印post请求参数,分析日志
2016-01-12 14:14
741 查看
有的时候服务器端接口允许请求的方式多样化且不过定,没有nginx的内网服务还需要统计分析post请求日志
1) 在项目中加入相应的包和类,加载那里无所谓,只要web.xml配置正确即可
package filters;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.*;
public final class PostDataDumperFilter implements Filter {
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 names = request.getParameterNames();
StringBuffer output=new StringBuffer();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
output.append(name+"=");
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("post", output);
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig)
throws ServletException {
this.filterConfig = filterConfig;
}
}
2) 配置web.xm,一般在路径 main/webapps/WEB-INF/web.xml:
<filter>
<filter-name>post-data-dumper-filter</filter-name>
<filter-class>filters.PostDataDumperFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>post-data-dumper-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3) now you have the attribute postdata for each request and you can easily log it in access valve, for example:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern='%h %p %H %l %u %t "%r" params={%{post}r} %s %bbytes %Dms' resolveHosts="false"/>
1) 在项目中加入相应的包和类,加载那里无所谓,只要web.xml配置正确即可
package filters;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.*;
public final class PostDataDumperFilter implements Filter {
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 names = request.getParameterNames();
StringBuffer output=new StringBuffer();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
output.append(name+"=");
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("post", output);
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig)
throws ServletException {
this.filterConfig = filterConfig;
}
}
2) 配置web.xm,一般在路径 main/webapps/WEB-INF/web.xml:
<filter>
<filter-name>post-data-dumper-filter</filter-name>
<filter-class>filters.PostDataDumperFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>post-data-dumper-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3) now you have the attribute postdata for each request and you can easily log it in access valve, for example:
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern='%h %p %H %l %u %t "%r" params={%{post}r} %s %bbytes %Dms' resolveHosts="false"/>
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- i-jetty环境搭配与编译
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- nginx代理指定目录
- 实现单Tomcat多Server配置
- 生产环境下的Tomcat配置
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- 访问Nginx发生SSL connection error的一种情况
- PropertyChangeListener简单理解
- Linux部署Tomcat服务器