Tomcat 7:RequestDumperFilter 未记录 HTTP 请求/响应正文

Posted

技术标签:

【中文标题】Tomcat 7:RequestDumperFilter 未记录 HTTP 请求/响应正文【英文标题】:Tomcat 7: RequestDumperFilter not logging HTTP Request/Response body 【发布时间】:2017-12-18 07:20:24 【问题描述】:

RequestDumperFilter 是一个 Tomcat 日志过滤器,用于记录 HTTP 请求和响应。

我已经根据this documentation为Tomcat设置了org.apache.catalina.filters.RequestDumperFilter

如日志所示,正在记录消息标头,但对于 HTTP 请求和响应(已使用各种消息进行测试),日志中省略了消息正文

文档提到了记录正文所需的logging.properties 设置:

## 级别必须至少设置为 INFO。如果您还想转储 body,请将其设置为 FINEST

org.apache.catalina.filters.RequestDumperFilter class in the Apache SVN repository 似乎没有记录消息正文的功能。

$catalina.base/conf/logging.properties:

handlers = 1catalina.org.apache.juli.FileHandler, 2localhost.org.apache.juli.FileHandler, 3manager.org.apache.juli.FileHandler, 4host-manager.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler, 1request-dumper.org.apache.juli.FileHandler

...

# To this configuration below, 1request-dumper.org.apache.juli.FileHandler
# also needs to be added to the handlers property near the top of the file

1request-dumper.org.apache.juli.FileHandler.level = FINEST
1request-dumper.org.apache.juli.FileHandler.directory = $catalina.base/logs
1request-dumper.org.apache.juli.FileHandler.prefix = request-dumper.
1request-dumper.org.apache.juli.FileHandler.formatter = org.apache.juli.VerbatimFormatter
org.apache.catalina.filters.RequestDumperFilter.level = FINEST
org.apache.catalina.filters.RequestDumperFilter.handlers = \
  1request-dumper.org.apache.juli.FileHandler

$catalina.base/conf/web.xml:

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

$catalina.base/logs/request-dumper.log:

http-bio-8080-exec-9 START TIME        =13-Jul-2017 15:08:40
http-bio-8080-exec-9         requestURI=/manager/html/upload
http-bio-8080-exec-9           authType=BASIC
http-bio-8080-exec-9  characterEncoding=UTF-8
http-bio-8080-exec-9      contentLength=615391
http-bio-8080-exec-9        contentType=multipart/form-data; boundary=----WebKitFormBoundaryP2bE0aVydgRqWbXP
http-bio-8080-exec-9        contextPath=/manager
http-bio-8080-exec-9             cookie=JSESSIONID=144935F6B43B3CB48AAD7E2980DC1E69
http-bio-8080-exec-9             header=host=localhost:8080
http-bio-8080-exec-9             header=connection=keep-alive
http-bio-8080-exec-9             header=content-length=615391
http-bio-8080-exec-9             header=cache-control=max-age=0
http-bio-8080-exec-9             header=authorization=Basic YWRtaW46Z29WYWx1ZTEyMyE=
http-bio-8080-exec-9             header=origin=http://localhost:8080
http-bio-8080-exec-9             header=upgrade-insecure-requests=1
http-bio-8080-exec-9             header=user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36
http-bio-8080-exec-9             header=content-type=multipart/form-data; boundary=----WebKitFormBoundaryP2bE0aVydgRqWbXP
http-bio-8080-exec-9             header=accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
http-bio-8080-exec-9             header=referer=http://localhost:8080/manager/html/undeploy?path=/ReqRespDumpFilter&org.apache.catalina.filters.CSRF_NONCE=DDA08F1F9D404EE9E5631E9CA9528401
http-bio-8080-exec-9             header=accept-encoding=gzip, deflate, br
http-bio-8080-exec-9             header=accept-language=en-US,en;q=0.8
http-bio-8080-exec-9             header=cookie=JSESSIONID=144935F6B43B3CB48AAD7E2980DC1E69
http-bio-8080-exec-9             locale=en_US
http-bio-8080-exec-9             method=POST
http-bio-8080-exec-9          parameter=org.apache.catalina.filters.CSRF_NONCE=80714B6B625B708F2BB416747BEFB87E
http-bio-8080-exec-9           pathInfo=/upload
http-bio-8080-exec-9           protocol=HTTP/1.1
http-bio-8080-exec-9        queryString=org.apache.catalina.filters.CSRF_NONCE=80714B6B625B708F2BB416747BEFB87E
http-bio-8080-exec-9         remoteAddr=0:0:0:0:0:0:0:1
http-bio-8080-exec-9         remoteHost=0:0:0:0:0:0:0:1
http-bio-8080-exec-9         remoteUser=admin
http-bio-8080-exec-9 requestedSessionId=144935F6B43B3CB48AAD7E2980DC1E69
http-bio-8080-exec-9             scheme=http
http-bio-8080-exec-9         serverName=localhost
http-bio-8080-exec-9         serverPort=8080
http-bio-8080-exec-9        servletPath=/html
http-bio-8080-exec-9           isSecure=false
http-bio-8080-exec-9 ------------------=--------------------------------------------
http-bio-8080-exec-9 ------------------=--------------------------------------------
http-bio-8080-exec-9           authType=BASIC
http-bio-8080-exec-9        contentType=text/html;charset=utf-8
http-bio-8080-exec-9             header=Content-Type=text/html;charset=utf-8
http-bio-8080-exec-9             header=Transfer-Encoding=chunked
http-bio-8080-exec-9             header=Date=Thu, 13 Jul 2017 13:08:40 GMT
http-bio-8080-exec-9         remoteUser=admin
http-bio-8080-exec-9             status=200
http-bio-8080-exec-9 END TIME          =13-Jul-2017 15:08:40
http-bio-8080-exec-9 ===============================================================

【问题讨论】:

【参考方案1】:

如你所说:

Apache SVN 存储库中的 org.apache.catalina.filters.RequestDumperFilter 类似乎没有记录消息正文的功能。

Tomcat 不记录消息正文(我也找不到该功能)。我认为包含在 OpenEdge 安装目录中的 Tomcat 是由 OpenEdge 开发人员自定义的。

Tomcat 官方文档中没有提到:

级别必须至少设置为 INFO。如果您还想转储 body,请将其设置为 FINEST

所以如果你想让 Tomcat 记录消息体,你可能需要自定义 RequestDumperFilter 类。

【讨论】:

【参考方案2】:

我使用 logback 记录完整的请求和响应以进行调试和分析。 它可以记录到日志文件或数据库。

【讨论】:

以上是关于Tomcat 7:RequestDumperFilter 未记录 HTTP 请求/响应正文的主要内容,如果未能解决你的问题,请参考以下文章

Fpm 之 tomcat-7.0.88 rpm 包定制

使用 Tomcat 7 的 tomcat-maven-plugin - tomcat:deploy 有效,tomcat:undeploy 无效

Apache Tomcat 7 安装与配置

我应该从 Tomcat 7 升级到 Tomcat8

CentOS-7.0.中安装与配置Tomcat-7的方法

CentOS-7.0.中安装与配置Tomcat-7的方法