Is it possible to print all requests to Tomcat and responses from Tomcat in a logfile?
ex:
request
headers: [
David Lee says add this to your server.xml
:
<Valve className="org.apache.catalina.valves.RequestDumperValve"/>
but, I guess that is tomcat 6; this answer shows how to use Request_Dumper_Filter
in tomcat 7 https://stackoverflow.com/a/8727615/1763984
https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Request_Dumper_Filter
The Request Dumper Filter logs information from the request
and response
objects and is intended to be used for debugging purposes.
The following entries in a web application's web.xml
would enable the Request Dumper filter for all requests for that web application.
If the entries were added to CATALINA_BASE/conf/web.xml
, the Request Dumper Filter would be enabled for all web applications
.
<filter>
<filter-name>requestdumper</filter-name>
<filter-class>
org.apache.catalina.filters.RequestDumperFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>requestdumper</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
Put an AccessLogValve
in the Host
or Context
element e.g.:
<Host name="www.mysite.com" appBase="..." >
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="mysitelog." suffix=".txt"
pattern="..." resolveHosts="false" />
</Host>
The pattern
attribute can take one of two shorthand values (common,combined) or a custom pattern using a number of constants and replacement strings. Let me quote the Tomcat docs:
https://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Access_Log_Valve
Values for the pattern attribute are made up of literal text strings, combined with pattern identifiers prefixed by the "%" character to cause replacement by the corresponding variable value from the current request and response. The following pattern codes are supported:
%a - Remote IP address %A - Local IP address %b - Bytes sent, excluding HTTP headers, or '-' if zero %B - Bytes sent, excluding HTTP headers %h - Remote host name (or IP address if enableLookups for the connector is false) %H - Request protocol %l - Remote logical username from identd (always returns '-') %m - Request method (GET, POST, etc.) %p - Local port on which this request was received. See also %{xxx}p below. %q - Query string (prepended with a '?' if it exists) %r - First line of the request (method and request URI) %s - HTTP status code of the response %S - User session ID %t - Date and time, in Common Log Format %u - Remote user that was authenticated (if any), else '-' %U - Requested URL path %v - Local server name %D - Time taken to process the request, in millis %T - Time taken to process the request, in seconds %F - Time taken to commit the response, in millis %I - Current request thread name (can compare later with stacktraces)
There is also support to write information incoming or outgoing headers, cookies, session or request attributes and special timestamp formats. It is modeled after the Apache HTTP Server log configuration syntax. Each of them can be used multiple times with different xxx keys:
%{xxx}i write value of incoming header with name xxx %{xxx}o write value of outgoing header with name xxx %{xxx}c write value of cookie with name xxx %{xxx}r write value of ServletRequest attribute with name xxx %{xxx}s write value of HttpSession attribute with name xxx %{xxx}p write local (server) port (xxx==local) or remote (client) port (xxx=remote) %{xxx}t write timestamp at the end of the request formatted using the enhanced SimpleDateFormat pattern xxx
As you can see there are quite a few fields that can be used, but if you still need more, you have to write your own AccessLogValve
implementation.