Solved

How to implement Client Specfic information in Log 4j

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

856 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