Solved

How to implement Client Specfic information in Log 4j

Posted on 2009-04-07
2
292 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 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