posted 17 years ago
[Patri]: First option, we tried with FileAppender . But we are not able get the logger messages to respective logger files. The logger messages are logging in jubmpling way to different loggers. Also we tried by giving different log4j properties for each thread. But we are getting the same result.
Well, any suggestion we make can be met with "it didn't work" - it may take effort to debug. It sounds like maybe the appenders are being run from different threads than the originating logging events. Perhaps you can include some calls to print the current value of Thread.currentThread().getName() to see if the threads used by the Appenders match the threads used in the code that makes the logging calls. If they don't match, then you can probably just apply the strategy I suggested at a different level. E.g. you can create a custom Logger rather than a custom Appender, and let the Logger keep a set of other Loggers in a ThreadLocal to look up the appropriate Logger for that thread.
You don't say anything about the first option I suggested. Well, you do begin your reply with "first option", but you appear to be talking about the second thing I suggested. It sounds like my first suggestion may well be simpler. Note that I'm not suggesting different log4j properties for each thread - I'm suggesting using a log4j format that will include info on the current thread. That should be pretty simple. Then write a program that reads the log file and splits it into separate files based on the value of the thread identifier.
Or, regarding the NDC suggestion:
[Patri]: With normal process, there will not be any problem. But if an exception occurs in the thread. It will exit the program in the middle. We cant apply the NDC stratagy in this situation.
Why not? You need to put calls to NDC.remove() and pop() into finally blocks to ensure they're called at appropriate times. That can be tedious and may be error prone, but it's not impossible. If you miss a call to pop(), that just means you'll have more info than you want; it's not the end of the world. And it will be info about what was going on when the error occurred, which quite possibly you want in the log anyway. Just make sure you call NDC.remove() when a thread (or Runnable) ends, so you don't get a memory leak. I haven't really used NDCs myself, so I'm not sure, but I don't see what your objection is here.
"I'm not back." - Bill Harding, Twister