Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log all headers of request/response in Tomcat 7

I'm trying to log all headers of request/response in Tomcat 7. I tried to use access-log-valve. But as mentioned in the link, we can print headers only one by one. We have to specify each and every header we need.

e.g.

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

Isn't there a way to log all headers at once, may be using a wildcard?

Thanks.

like image 219
Bee Avatar asked Jul 09 '13 10:07

Bee


4 Answers

Inital important hint, especially for production environments: be careful enabling the filter since it may harm your request param encoding! So maybe be sure to set up everything right by maybe setting up the org.apache.catalina.filters.SetCharacterEncodingFilter first!


From Tomcat 7+ on you should do it via filters:

  • in 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>

and then log it e.g. to some special log file like this in CATALINA_BASE/conf/logging.properties (modified based on 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

Till Tomcat 6 and before you could use the RequestDumperValve (as in Brian's answer):

add this to your server.xml:

<Valve className="org.apache.catalina.valves.RequestDumperValve"/>
like image 55
Andreas Covidiot Avatar answered Nov 13 '22 00:11

Andreas Covidiot


There's no way out of the box - but an easy alternative is to stuff run a filter. The filter shove's all the headers you want into the servlet request. Then use "%{foo}r" in your pattern where foo is the name of the variable in the servlet request.

like image 37
Tim Funk Avatar answered Nov 12 '22 23:11

Tim Funk


David Lee says add this to your server.xml:

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

like image 1
Brian Tingle Avatar answered Nov 13 '22 00:11

Brian Tingle


Add this to your 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>

This will log request and response headers in cataline.out log file:

==> /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 ===============================================================

Tested on Tomcat 8 on Centos 7.

like image 1
user674669 Avatar answered Nov 13 '22 00:11

user674669