tags:

views:

23

answers:

2

In our grails application we're logging a lot, but need a mechanism to associate all of those messages with the request/response being processed. It has proven easy enough to generate a request UUID, but now I'd like that id appended to each log message generated within a request context without passing that id within each log message. Has anybody implemented such a system so that you can associate all of your log statements together?

A: 

you could try utilizing the 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, you can get whatever identifier you have stashed away?

Aaron Saunders
Thanks for the recommendation, but I have a solution for this problem. I should have been more specific with my question. I don't have a problem from all of my log statements inserting the requestId and can store that I'd within the request context. Rather I'd like to avoid having to insert the requestId in all of my log statements but have them inserted automatically much like other context.
Chris Alef
+1  A: 

A rather obscure feature of log4j, 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 work fine in Grails as well if you use a custom AppFilter to set the request-unique value.

ZbigniewC
That looks like exactly what I need. Thank you!
Chris Alef