logging session ID in each entry

Hi all,

I'm trying to log the session ID in each log entry, so that I'm able to see which log entry belonged to which session when analysing the log file later.

I'm using log4j and I think the right way to do stuff like this is to push the session ID to the NDC (nested diagnostic context, see http://logging.apache.org/log4j/docs/api/org/apache/log4j/NDC.html).

Now I'm having trouble to find the right place where to push this ID to the NDC and when to remove it again. The NDC is thread based, but I'm not sure how this fits into the JSF model.

I tried to use PhaseListeners, one before the RESTORE_VIEW (NDC.push()), and one after RENDER_RESPONSE (NDC.remove()), but this leads to multiple session IDs being in the NDC after a few requests.

Any idea how to fix this cleanly? Or any other approach?

Thanks a lot,

Gunnar

[876 byte] By [GunnarSa] at [2007-11-27 5:52:30]
# 1

Well, I'm familiar with log4j and use it in most of my projects... But I'm not familiar enough with it to help you out directly regarding the NDC.

I have another suggestion for you though. You can always store the session id in a session bean, and retrieve the value each time you log output. Each unique visitor to your site will have their own session scope. So it is safe to assume that the session id retrieved from their session is unique to them (assuming you have a mechanism to generate a unique session ID amongst all users).

CowKing

IamCowKinga at 2007-7-12 15:43:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

CowKing,

thanks for your reply. I already keep the session ID (along with other data) in a session bean, but I don't want to explicitly output the ID - I had to change all calls to the logger, and I wouldn't be able to simply turn this on or off (and many other more disadvantages) - see also http://wiki.apache.org/logging-log4j/NDCvsMDC for more info about NDC, if you're interested.

I've been searching the web quite a while now, and I hardly find anything useful. I'm really irritated because I believe this is a quite important thing to have in a multi-client application.

Rgds,

Gunnar

GunnarSa at 2007-7-12 15:43:35 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...