Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

log4j help needed  RSS feed

 
Ranch Hand
Posts: 431
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,

In my project log4j is used extensively. Almost in all the action classes (we use struts), we log things. But the problem is, when concurrent users are there the logs are getting mixed up. Because of this debugging becomes a head-ache. I want to prefix all the logging statments with the userid of the person who has logged in. But its not possible to do this change in all the places where logging is used since I need to compile and deploy the whole application again which is impossible. Is there any way by which I can change some class or config file through which I can achieve my requirement?
 
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can achieve this through some code and through some configuration. Have a look at the NDC/MDC section at:

Log4j article
Log4j MDC article

You will find a code sample(MDCUserServletFilter.java) at:
Code sample

Your log4j.xml will look like:
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!