How to associate all logs with their request in grails?

We register a lot in our grails application, but we need a mechanism to link all of these messages to the processed request / response. It is easy enough to create a UUID request, but now I would like this identifier to be added to each log message generated in the context of the request, without transmitting this identifier in each log message. Has anyone implemented such a system so that you can bind all your logical operators together?

+4
source share
2 answers

The rather obscure log4j feature called MDC seems to be exactly what you need.

Something like http://gustlik.wordpress.com/2008/07/05/user-context-tracking-in-log4j/

It will do a great job in Grails if you use a custom AppFilter to set a unique request value.

+4
source

you can try using RequestContextHolder

http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/context/request/RequestContextHolder.html

ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); log.debug attr.getRequest().getSession() 

Once you get the session object, can you get any identifier that you set aside?

0
source

All Articles