Building out a new API using Grails 4, and I would like to have the option of logging the full request (headers, method, content, etc). I can see the request in an Interceptor, but the content can only be read once (using HttpServletRequest.getInputStream()
), so reading it in the Interceptor prevents the content from being available in the Controller.
There are some similar questions about this on Stack Overflow already that address this need by using Grails Filters.
One reason I don't want to go down that path is that, according to the Grails docs, Filters are now considered deprecated (as of v3.0), and Interceptors should instead be used. Unfortunately, none of the solutions I can find work with Interceptors. I tried a couple of those solutions myself that involve wrapping the request inside of a HttpServletRequestWrapper
to cache the body content and ran into the same issues as others with trying to get it to work with an Interceptor.
I have seen suggestions to use Java Servlet Filters, it's not obvious how that's different from Grails Filters, or if they should also be avoided.
Edit: As noted in comments below, I didn't mention that I'm using command objects, and the solution needs to work with those. I'm also using a number of other Grails features that I'm not sure will be affected by whatever solutions will be proposed, so if there are limitations to the proposed solution, it would be good to know about those.