Solved

How to implement Client Specfic information in Log 4j

Posted on 2009-04-07
2
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…

688 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