Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
729 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...