是否可以将所有发送到Tomcat和从Tomcat返回的请求都打印到日志文件中?
例如:
请求
头部:[header1=a,header2=a]
参数:[param1=avv,param2=b]
响应
状态码 = 200
响应 = 它的工作
是否可以将所有发送到Tomcat和从Tomcat返回的请求都打印到日志文件中?
例如:
请求
头部:[header1=a,header2=a]
参数:[param1=avv,param2=b]
响应
状态码 = 200
响应 = 它的工作
在Host
或Context
元素中放置一个AccessLogValve
,例如:
<Host name="www.mysite.com" appBase="..." >
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs" prefix="mysitelog." suffix=".txt"
pattern="..." resolveHosts="false" />
</Host>
< p > pattern
属性可以采用两个速记值 (common,combined) 或使用多个常量和替换字符串的自定义模式。让我引用一下 Tomcat 文档:
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
正如您所看到的,有很多可以使用的字段,但如果您仍需要更多,您必须编写自己的AccessLogValve
实现。
https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html#Request_Dumper_Filter
请求转储过滤器记录来自请求
和响应
对象的信息,旨在用于调试目的。
在Web应用程序的web.xml
中添加以下条目将为该Web应用程序的所有请求启用请求转储过滤器。
如果这些条目被添加到CATALINA_BASE/conf/web.xml
中,则请求转储过滤器将会为所有Web应用程序
启用。
<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>
David Lee说将以下内容添加到您的server.xml
文件中:
<Valve className="org.apache.catalina.valves.RequestDumperValve"/>
但是,我猜这是针对Tomcat 6的;这个答案展示了如何在Tomcat 7中使用Request_Dumper_Filter
https://dev59.com/l2035IYBdhLWcg3wBLNL#8727615