gpt4 book ai didi

tomcat - 记录Tomcat Server的所有HTTP请求?

转载 作者:行者123 更新时间:2023-11-28 21:46:20 26 4
gpt4 key购买 nike

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

例如:

request

headers: [header1=a, header2=a]

params: [param1=avv, param2=b]

response

status-code = 200

response = its works

最佳答案

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>

pattern 属性可以采用两个速记值之一(commoncombined)或使用多个常量和替换的自定义模式字符串。让我引用 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 实现。

关于tomcat - 记录Tomcat Server的所有HTTP请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34094030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com