记录Tomcat服务器的所有HTTP请求? [英] Log all HTTP requests of Tomcat Server?

查看:600
本文介绍了记录Tomcat服务器的所有HTTP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to print all requests to Tomcat and responses from Tomcat in a logfile?

例如:

请求

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

headers: [header1=a, header2=a]

params:[param1 = avv,param2 = b]

params: [param1=avv, param2=b]

响应

状态码= 200

响应=它的作品

推荐答案

HostContext元素中放置一个AccessLogValve,例如:

Put an AccessLogValve in the Host or Context element e.g.:

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

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

</Host> 

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

The pattern attribute can take one of two shorthand values (common,combined) or a custom pattern using a number of constants and replacement strings. Let me quote the Tomcat docs:

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

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

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)

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

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实现.

As you can see there are quite a few fields that can be used, but if you still need more, you have to write your own AccessLogValve implementation.

这篇关于记录Tomcat服务器的所有HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆