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

13

是否可以将所有发送到Tomcat和从Tomcat返回的请求都打印到日志文件中?

例如:

请求

头部:[header1=a,header2=a]

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

响应

状态码 = 200

响应 = 它的工作


请查看您的 server.xml 文件,以获取 access.log 中响应状态的模式。要获取头文件和其他细节,您可能需要编写自己的阀门。 - SMA
对于使用IntelliJ IDEA的开发人员,可以在日志标签 § 运行/调试配置:Tomcat服务器中启用"Tomcat本地主机访问日志",这有助于查看有关传入请求的摘要。您可能还想了解Tomcat中的本地主机访问日志是什么? - li ki
3个回答

20

HostContext元素中放置一个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实现。


8
不支持记录所有标题吗?(这基本上是提出的问题) - peterh
https://dev59.com/j2Mm5IYBdhLWcg3whfWe - Brian Tingle
@peterh:基于Tomcat 6和7+的请求转储程序,附带示例:https://dev59.com/j2Mm5IYBdhLWcg3whfWe#54595660 - Andreas Covidiot
如果我配置了两个连接器,那么是否可以记录请求是通过HTTP还是HTTPS发送的? - Diego Ramos

16

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>

感谢您提供的出色答案。请求转储程序提供了有关每个请求的大量信息,也许太多了,但如果我正在调试,则更喜欢这么多信息。只需注意在完成调试时需要删除它,除非您想要一些非常大的日志文件。 - Gary Sheppard
请记得按照文档 http://tomcat.apache.org/tomcat-9.0-doc/config/filter.html#Request_Dumper_Filter 中的说明添加日志记录配置。对我来说,这部分在 tomcat9 下的工作非常重要:“CATALINA_BASE/conf/logging.properties 中的以下条目…” - marco bonfigli
你知道如果Tomcat托管在Windows服务器上是否需要执行特殊操作吗?我的意思是,当服务器是Linux时,我能够使用过滤器,但尝试使用Windows时,它没有创建request-dumper文件。但我不确定这是否是因为我的应用程序配置,顺便说一下,该应用程序是SAP BO BI。 - federico

0

David Lee说将以下内容添加到您的server.xml文件中:

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

但是,我猜这是针对Tomcat 6的;这个答案展示了如何在Tomcat 7中使用Request_Dumper_Filter https://dev59.com/l2035IYBdhLWcg3wBLNL#8727615


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接