Grails on Tomcat - How to log raw HTTP request/res

2019-05-14 18:40发布

I couldn't find the way to configure my dummy tutorial Grails application to log all HTTP requests and responses accepted/generated by Grails server (actually - Tomcat). Is this possible?

3条回答
来,给爷笑一个
2楼-- · 2019-05-14 18:58

Such logging could easily be implemented as a filter - either a Grails filter (see http://grails.org/doc/latest/guide/theWebLayer.html#filters for description of those and an example logging filter), or as a standard servlet filter (an example can be found here: http://www.java2s.com/Code/Java/Servlets/LogFilter.htm).

If you choose the second option, you'll have to modify web.xml to include your filter. To do so, execute grails install-templates. This will copy standard web.xml to templates/WEB-INF directory in your project, and you can modify it there.

查看更多
The star\"
3楼-- · 2019-05-14 18:59

Another option would be to use tomcat's built in access logging.

http://tomcat.apache.org/tomcat-6.0-doc/config/valve.html#Access_Log_Valve

Im using this myself, and it is quite configurable.

How to configure tomcat 6

 <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
               prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>

Add this to the end of your server.xml config under the tag "Host".

查看更多
狗以群分
4楼-- · 2019-05-14 19:03

you could either use a Grails plugin: https://github.com/TouK/grails-httplogger or a Tomcat filter.
If you use Tomcat 7, it is the RequestDumperFilter. I prefer the RequestDumperFilter.

In short:

Install the Grails templates

    grails install-templates

Edit web.xml and add the RequestDumperFilter

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

Edit the log4j section in Config.groovy and add

debug   'org.apache.catalina.filters.RequestDumperFilter'

This should be sufficient.

If you need some more information, here you will find an extensive instruction.

查看更多
登录 后发表回答