I would like to add logging of requests and responses to a Jav开发者_如何学运维a webapp which I am running within Tomcat 6. The intention is to produce a log file with output similar to what Wireshark would display. For example:
GET / HTTP/1.1
Host: www.bing.com
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
I thought the best approach would be to use a Filter to capture the incoming HttpServletRequest and wrap the HttpServletResponse to allow the response content to be captured.
However, once I have an HttpServletRequest and HttpServletResponse is there a standard way to pretty print these objects out as they appeared on the wire?
Tomcat supports so called Log Valve - with a bit of formatting you can achieve what you want.
Also Logback Access module aims to provide the same functionality but using Logback library.
I don't know if you can display the headers exactly as they would be sent, but if you enable Tomcat's request dumper valve you will see all of the headers sent and received.
精彩评论