my dog learned polymorphism*
The moose likes Other Open Source Projects and the fly likes LOG4J Appending Username in log Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "LOG4J Appending Username in log" Watch "LOG4J Appending Username in log" New topic

LOG4J Appending Username in log

Karthikeyan Ganapathy

Joined: May 12, 2012
Posts: 1
I just want to know how to append a String in a log using Log4j, in my application we have multiple users, so i just want to append the Username along with the log, so that i can differentiate the logs based on the user, Help me in find out how to append the username along with logs, not through each and every class, should be at some log4j class
Guillaume Jourdan
Ranch Hand

Joined: Jul 24, 2012
Posts: 47

You can use the log4J mapped diagnostic context (MDC)

You can define the username in your user thread code :

And add it in your appender pattern configuration, example :

Warning :

MDC Javadoc wrote:The MDC is managed on a per thread basis. A child thread automatically inherits a copy of the mapped diagnostic context of its parent.


Don't get me started about those stupid light bulbs.
subject: LOG4J Appending Username in log
Similar Threads
Tomcat logging
persist previous server.log after server restarts using log4j.xml
Log4j configuration help. Using different log files for different applications
Need to append userid in logs using log4j
Remote Logging Error