如何在Tomcat 7中记录请求/响应的所有标头

时间:2013-07-09 10:17:47

标签: tomcat logging tomcat7

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

e.g。

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

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

感谢。

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

初始重要提示,特别是针对生产环境:请小心启用过滤器,因为它可能会损害您的请求参数编码< / strong>!因此,也许一定要先设置org.apache.catalina.filters.SetCharacterEncodingFilter来正确设置所有内容!


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

  • 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 docs进行修改):

## 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 (与Brian's answer相同):

将此添加到您的 server.xml

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

答案 2 :(得分:1)

David Lee说将此添加到您的server.xml

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

答案 3 :(得分:0)

将此添加到您的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上进行了测试。