记录Tomcat服务器的所有HTTP请求?

时间:2015-12-04 17:25:27

标签: tomcat logging request

是否可以在日志文件中打印Tomcat的所有请求和Tomcat的响应?

例如:

  
    

请求

         

标题:[header1 = a,header2 = a]

         

参数:[param1 = avv,param2 = b]

         

响应

         

status-code = 200

         

响应=其作品

  

3 个答案:

答案 0 :(得分:15)

AccessLogValve放入HostContext元素中,例如:

<Host name="www.mysite.com" appBase="..." >

    <Valve className="org.apache.catalina.valves.AccessLogValve"
     directory="logs" prefix="mysitelog." suffix=".txt" 
     pattern="..." resolveHosts="false" />

</Host> 

pattern属性可以使用两个速记值之一(常用组合)或使用多个常量和替换字符串的自定义模式。让我引用Tomcat文档:

https://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Access_Log_Valve

  

pattern属性的值由文字文本字符串组成,   与前缀为“%”字符的模式标识符相结合   导致从当前的相应变量值替换   请求和回复。支持以下模式代码:

%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)
     

还支持写入传入或传出的信息   标头,cookie,会话或请求属性以及特殊时间戳   格式。它是在Apache HTTP Server日志配置之后建模的   句法。它们中的每一个都可以使用不同的xxx多次   键:

%{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

正如您所看到的,可以使用很多字段,但如果您还需要更多字段,则必须编写自己的AccessLogValve实现。

答案 1 :(得分:2)

https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Request_Dumper_Filter

请求转储程序过滤器记录来自requestresponse对象的信息,旨在用于调试目的。

Web应用程序的web.xml中的以下条目将为该Web应用程序的所有请求启用Request Dumper过滤器。

如果将条目添加到CATALINA_BASE/conf/web.xml,则会启用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>

答案 2 :(得分:-1)

David Lee说将此添加到您的server.xml

<Valve className="org.apache.catalina.valves.RequestDumperValve"/>

但是,我猜那是tomcat 6;这个答案显示了如何在tomcat 7 https://stackoverflow.com/a/8727615/1763984

中使用Request_Dumper_Filter