Solved

How to implement Client Specfic information in Log 4j

Posted on 2009-04-07
2
286 Views
Last Modified: 2012-05-06
Hi all,
         We have web application which is using Log4j for logging . But there is no client specific information in each statement  of log file. But we want to implememnt a client specific information in our logging system. Could you please tell me how to implement the NDC or any others on Log4j.
0
Comment
Question by:balatrue
2 Comments
 
LVL 46

Expert Comment

by:tbsgadi
ID: 24087755
Have a look at the following:

http://wiki.apache.org/logging-log4j/NDCvsMDC

Gary
0
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 500 total points
ID: 24088366
I usually use the MDC to do this. Basically you've got to figure out somewhere in your application call path to add the information to the MDC and then where to remove it. MDC is essentially a ThreadLocal Map. Each thread of execution in the JVM will have its own Map of values. The problem is, if this is a web application then Servlet threads get reused. If you put data in the MDC and don't clear it at some point then when that thread is reused there will be stale values there. Again, if this is a web application, and you know the values to put into the MDC fairly early on, then you might consider implementing a Filter that does this for you. I do this a lot when I want to add SessionID the log.

So that's the hard part. Where do you put and clear the MDC. The easy part is, in your Appender's PatternLayout specification, add something like the following to the string: %X{mdcKey} where mdcKey is the key name of the value as you added it to the MDC. An example ... if you do this in your code:
    MDC.put("ClientID", clientID);

Then your pattern specification might look like this:
    [%t] %-5p %c %X{ClientID} - %m%n

Regards,
Jim
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 62
Opening PDF on button click and fill new document 2 35
Scientific articles behind a login? 6 26
ebay api to see some data about my customers 2 28
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This video teaches viewers about errors in exception handling.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question