Reputation: 165
I want to log the current user of my web app. I tried this with an own implementation of the StrLookup interface where i call something like Environment.getCurrentUser(), but the lookup is executed just once during the initiatization of the logger. Is there a thread specific lookup functionality, that will be executed every time, like the keywords in patterns?
Upvotes: 0
Views: 117
Reputation: 9141
What I do is use a servlet filter or a Spring Interceptor and add items to the Log4j2 ThreadContext before the request and then remove them after the request. This will cause the information to be included in every logging event and can be referenced in the pattern layout via the %X converter.
See http://logging.apache.org/log4j/2.x/manual/eventlogging.html for an example.
This can be useful if you want to write an application that has to be sensitive to the customer or client involved but you don't want to have to pass a customer or client id through every method.
Upvotes: 1