aspose file tools*
The moose likes Tomcat and the fly likes How to configure Tomcat to log POST data? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "How to configure Tomcat to log POST data?" Watch "How to configure Tomcat to log POST data?" New topic
Author

How to configure Tomcat to log POST data?

Janine Sisk
Greenhorn

Joined: Feb 25, 2010
Posts: 2
I want to use JMeter's access log sampler, to generate load tests based on real-world usage data:

http://jakarta.apache.org/jmeter/usermanual/jmeter_accesslog_sampler_step_by_step.pdf

But it requires that POST data be logged, and I'm stuck trying to figure out how to do that. It refers you to a page which no longer exists, but the modern version of it appears to be

http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html

But it doesn't give any info about POST data. I found the page for the ExtendedAccessLogValve

http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/valves/ExtendedAccessLogValve.html

And it looks like if I wanted to capture a particular named parameter I would use x-P(parameter-name) to do it, but that still doesn't help me get them all.

Is there a way to do this that I'm just not finding? Or is there a better tool out there that doesn't have this requirement?

Thanks in advance,

janine
Misha Ver
Ranch Hand

Joined: Mar 03, 2008
Posts: 470
Janine Sisk wrote:http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html

But it doesn't give any info about POST data.


There is value for pattern attribute. Perhaps you could you it.
Janine Sisk
Greenhorn

Joined: Feb 25, 2010
Posts: 2
I think that just prints GET or POST - I need to get the actual data sent with the form. But I can't use the x-P() pattern because I don't know the names of all the values being passed.
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Does any one have any idea how to get the post data?

My assignment is to create a log of all the access requests into TOMCAT and this log file should be used to replay for test case for load testing.

Now my urls in access log is just include URL with query string how to get the post data included in the log file so I can just reply the who log for testing?

Is it possible?


But this does not provide post data please help any comments appreciated thanks


SCJP, SCWCD, SCBCD, SCEA 5
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
I suppose you could create your own java.servlet.Filter implementation to extract the complete request body from POSTs to a file.

Seems like sombody should have already done this somewhere, try searching.

Bill
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
Hello William

Thanks for this, but I am thinking there should be some out of the box method to retrieve the POST data from the request provided by apache as I am getting the query string information and URI.

There should be no reason to implement a servlet filter for this?

What do you think?, I have searched quite a lot and cannot find this informaiton ?

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Perhaps I am not understanding you - at what point do you want to intercept communication between client and server?

Are you in control on the client side or the server side?

You do realize that you can do a javax.servlet.http.getReader or getInputStream to get the full body of a request, right? But note that if you do the servlet will not be able to parse the parameters of the POST because the stream will already have been read. Thats why a special filter would be needed to both capture the body AND submit the request properly to the servlet.

Bill
Mohamed Farouk
Ranch Hand

Joined: Jun 08, 2005
Posts: 249
My assignment is to create a log of all the access requests into TOMCAT and this log file should be used to replay for test case for load testing.


Now my urls in access log is just include URL with query string how to get the post data included in the log file so I can just reply the who log for testing.

I dont want to write anything new this is what i get in a the tomcat access log4j configured file, I just need another configuration to get the post data so i can reply the request? Do you need any other information?

2010-07-21 16:57:07,709 catalina-exec-984 INFO [jira.web.filters.AccessLogFilter] gilla http://jira.xxxxx.echonet/secure/WorkflowUIDispatcher.jspa 5085917 21
2010-07-21 16:57:07,852 catalina-exec-970 INFO [jira.web.filters.AccessLogFilter] gilla http://jira.xxxxx.echonet/secure/CommentAssignIssue!default.jspa 5085917-7932 134
I am trying to find out is there any tomcat settings to get the POST data?
John Jr Wayne
Greenhorn

Joined: Jan 28, 2011
Posts: 1
I did it in this way:

1) Add the following class under WEB-INF/classes/filter/
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("postdata", output);
chain.doFilter(request, response);
}

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
}

2) add the filter conf in 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="access." suffix=".log" pattern='%h %p %H %l %u %t "%r" [%{postdata}r] %s %b %T' resolveHosts="false"/>

N.B. It does not work for multipart/related Posts
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to configure Tomcat to log POST data?