3

I am trying to use a Mapped Diagnostic Context to add the username of the user making a page request to all relevant logging statements. However I have tried three different ways to make it work without success:

  1. Pushing the username into the MDC after login and removing after logout. This method ends up mixing up which logging statement came from which user.
  2. Using a ServletFilter to push the username into the MDC on each page load and pop it back off as the request ends. This only catches some of the data and only in Spring security layer.
  3. Using a AOP @Around interceptor in front of all the Controller methods flat out didn't work.

Does anyone have any suggestions on how to make this happen?

4

1 回答 1

3

MDC有什么问题?Spring安全层是什么意思?我在一个 Web 应用程序中使用了这种方法,并且效果很好。因为 MDC 绑定到线程,所以来自该线程的所有日志记录语句都将设置用户名,即服务和存储库层也是如此。

当然,如果某些用户是通过使用线程(例如 servlet 3.0 异步处理、JMS 侦听器、执行器)来服务的,那么您将需要另一种方法在池线程中将用户名注入 MDC。

另请参阅我的答案here

于 2011-07-19T19:31:57.033 回答