Solved

How to implement Client Specfic information in Log 4j

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now