tomcat的acess_log打印post请求参数,分析日志

本文详细介绍了如何通过加入特定的Java过滤器和配置Web.xml文件来收集并分析服务器端接口的POST请求日志,进而便于进行深入的数据分析和故障排查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有的时候服务器端接口允许请求的方式多样化且不过定,没有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  IOExceptionServletException { 
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
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值