简体   繁体   中英

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

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?

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.

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

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM