tags:

views:

124

answers:

2

I've managed to enable access logging in Tomcat by editing the conf/server.xml and uncommenting the entry for the org.apache.catalina.valves.AccessLogValve Valve. I'd like to be able to dump the contents of POST's payload. It doesn't seem like any of the options in the pattern will do this. Is there a built in way to do this? Do I use the AccessLogValve?

+2  A: 

If the post is a form (application/x-www-urlencoded), you can use ExtendedAccessLogValve,

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

You have to select individual parameters in the pattern like this,

  x-P(param_name)
ZZ Coder
@ZZ: what if it is not a form?
Stephen C
If it's not a form, you can't really log the post body. The log format is defined by http://www.w3.org/TR/WD-logfile.html. It doesn't support arbitrary post body, which can be binary and huge, in case of file upload.
ZZ Coder
Unfortunately, this is not the case for me. I'm not posting content as parameters, though the payload size will not likely be more than a few kb.
Tim
Post payload doesn't belong to logs. You need to dump it somewhere else in your server.
ZZ Coder
Which is fine, but I want to be able to associate the access information for it. Is there some trick to doing this without writing a custom Valve or the like?
Tim
I don't think you will find anything inside Tomcat. If this is indeed for logging, you need to come up with a log format and hex-dump the request in your server. If it's for debugging, use Wireshark or Fiddler to watch the post body.
ZZ Coder
A: 

Since there was not a suggestion of a built in way to get the POST payload, I went ahead and wrote a custom filter that dumps the contents of the payload. Specifically:

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
                     FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    LOG.debug("payload: " + requestWrapper.getRequestBody());

and web.xml:

<filter>
    <filter-name>PayloadLoggingFilter</filter-name>
    <filter-class>com.host.PayloadLoggingFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>PayloadLoggingFilter</filter-name>
    <url-pattern>/resources/*</url-pattern>
</filter-mapping>
Tim