18

我正在尝试在 Tomcat 7 中记录所有请求/响应的标头。我尝试使用access-log-valve。但是正如链接中提到的,我们只能一个一个地打印标题。我们必须指定我们需要的每个标题。

例如

pattern="%{User-Agent}i %{Content-Type}i %{Accept}i %{Accept-Encoding}i
%{Accept-Language}i %{Accept-Charset}i %r %h %q"

有没有办法一次记录所有标题,可能是使用通配符?

谢谢。

4

4 回答 4

8

初始重要提示,尤其是对于生产环境:小心启用过滤器,因为它可能会损害您的请求参数编码!所以也许一定要先设置org.apache.catalina.filters.SetCharacterEncodingFilter来正确设置一切!


Tomcat 7 +开始,您应该通过过滤器进行操作:

  • 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/conf/logging.properties(基于Tomcat 7 文档修改):

## this may likely be your (to-be-updated) orig handlers declaration:
#handlers = \
#   1catalina.org.apache.juli.AsyncFileHandler\
#  ,2localhost.org.apache.juli.AsyncFileHandler\
#  ,3manager.org.apache.juli.AsyncFileHandler\
#  ,4host-manager.org.apache.juli.AsyncFileHandler\
#  ,java.util.logging.ConsoleHandler

## ... and you have to add your 1request-dumper:
handlers = \
   1request-dumper.org.apache.juli.FileHandler\
  ,1catalina.org.apache.juli.AsyncFileHandler\
  ,2localhost.org.apache.juli.AsyncFileHandler\
  ,3manager.org.apache.juli.AsyncFileHandler\
  ,4host-manager.org.apache.juli.AsyncFileHandler\
  ,java.util.logging.ConsoleHandler


## ...


# 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 = INFO
1request-dumper.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1request-dumper.org.apache.juli.FileHandler.prefix = localhost_access_log.req-dumps.
1request-dumper.org.apache.juli.FileHandler.encoding = UTF-8
1request-dumper.org.apache.juli.FileHandler.formatter = org.apache.juli.VerbatimFormatter
org.apache.catalina.filters.RequestDumperFilter.level = INFO
org.apache.catalina.filters.RequestDumperFilter.handlers = \
  1request-dumper.org.apache.juli.FileHandler

直到Tomcat 6之前,你可以使用RequestDumperValve(如布赖恩的回答):

将此添加到您的server.xml

<Valve className="org.apache.catalina.valves.RequestDumperValve"/>
于 2019-02-08T15:32:50.340 回答
4

没有开箱即用的方法 - 但一个简单的替代方法是运行过滤器。过滤器将您想要的所有标头推送到 servlet 请求中。然后在您的模式中使用“%{foo}r”,其中 foo 是 servlet 请求中变量的名称。

于 2013-11-01T18:21:43.213 回答
1

大卫李说把这个添加到你的server.xml

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

于 2017-02-14T22:58:45.760 回答
1

将此添加到您的 web.xml:

<filter>
    <filter-name>Request Dumper Filter</filter-name>
    <filter-class>org.apache.catalina.filters.RequestDumperFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Request Dumper Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

这将在 cataline.out 日志文件中记录请求和响应标头:

==> /usr/local/tomcat/logs/catalina.out <==
2020-06-10 12:20:47.844 INFO  [RequestDumperFilter] - http-nio-8080-exec-9 START TIME        =10-Jun-2020 12:20:47
2020-06-10 12:20:47.845 INFO  [RequestDumperFilter] - http-nio-8080-exec-9         requestURI=/myservice/health
2020-06-10 12:20:47.845 INFO  [RequestDumperFilter] - http-nio-8080-exec-9           authType=null
2020-06-10 12:20:47.846 INFO  [RequestDumperFilter] - http-nio-8080-exec-9  characterEncoding=null
2020-06-10 12:20:47.846 INFO  [RequestDumperFilter] - http-nio-8080-exec-9      contentLength=-1
2020-06-10 12:20:47.847 INFO  [RequestDumperFilter] - http-nio-8080-exec-9        contentType=null
2020-06-10 12:20:47.847 INFO  [RequestDumperFilter] - http-nio-8080-exec-9        contextPath=/myservice
2020-06-10 12:20:47.848 INFO  [RequestDumperFilter] - http-nio-8080-exec-9             header=host=192.168.99.200:9080
2020-06-10 12:20:47.849 INFO  [RequestDumperFilter] - http-nio-8080-exec-9             header=accept=text/plain, text/*, */*
2020-06-10 12:20:47.849 INFO  [RequestDumperFilter] - http-nio-8080-exec-9             header=accept-encoding=gzip
2020-06-10 12:20:47.850 INFO  [RequestDumperFilter] - http-nio-8080-exec-9             header=connection=close
2020-06-10 12:20:47.850 INFO  [RequestDumperFilter] - http-nio-8080-exec-9             locale=en_US
2020-06-10 12:20:47.851 INFO  [RequestDumperFilter] - http-nio-8080-exec-9             method=GET
2020-06-10 12:20:47.851 INFO  [RequestDumperFilter] - http-nio-8080-exec-9           pathInfo=null
2020-06-10 12:20:47.851 INFO  [RequestDumperFilter] - http-nio-8080-exec-9           protocol=HTTP/1.1
2020-06-10 12:20:47.852 INFO  [RequestDumperFilter] - http-nio-8080-exec-9        queryString=null
2020-06-10 12:20:47.852 INFO  [RequestDumperFilter] - http-nio-8080-exec-9         remoteAddr=172.18.0.1
2020-06-10 12:20:47.853 INFO  [RequestDumperFilter] - http-nio-8080-exec-9         remoteHost=172.18.0.1
2020-06-10 12:20:47.853 INFO  [RequestDumperFilter] - http-nio-8080-exec-9 requestedSessionId=null
2020-06-10 12:20:47.854 INFO  [RequestDumperFilter] - http-nio-8080-exec-9             scheme=https
2020-06-10 12:20:47.854 INFO  [RequestDumperFilter] - http-nio-8080-exec-9         serverName=192.168.99.200
2020-06-10 12:20:47.855 INFO  [RequestDumperFilter] - http-nio-8080-exec-9         serverPort=443
2020-06-10 12:20:47.855 INFO  [RequestDumperFilter] - http-nio-8080-exec-9        servletPath=/health
2020-06-10 12:20:47.856 INFO  [RequestDumperFilter] - http-nio-8080-exec-9           isSecure=true
2020-06-10 12:20:47.856 INFO  [RequestDumperFilter] - http-nio-8080-exec-9 ------------------=--------------------------------------------
2020-06-10 12:20:47.857 INFO  [RequestDumperFilter] - http-nio-8080-exec-9 ------------------=--------------------------------------------
2020-06-10 12:20:47.857 INFO  [RequestDumperFilter] - http-nio-8080-exec-9           authType=null
2020-06-10 12:20:47.857 INFO  [RequestDumperFilter] - http-nio-8080-exec-9        contentType=application/json;charset=UTF-8
2020-06-10 12:20:47.858 INFO  [RequestDumperFilter] - http-nio-8080-exec-9             status=200
2020-06-10 12:20:47.858 INFO  [RequestDumperFilter] - http-nio-8080-exec-9 END TIME          =10-Jun-2020 12:20:47
2020-06-10 12:20:47.858 INFO  [RequestDumperFilter] - http-nio-8080-exec-9 ===============================================================

在 Centos 7 上的 Tomcat 8 上测试。

于 2020-06-10T12:36:29.480 回答