| Author |
Logging and session values
|
Renato Losio
Ranch Hand
Joined: Nov 23, 2005
Posts: 99
|
|
Hello, I have a logging problem that I can't sort out in a simple way and I think is quite common. As I have many concurrent users doing with different transactions, I would like to identify every single line that I log with an id that helps me to identify who the user was. May be the session id (but note that some classes that log don't have access to the context or any 'standard' shared resource. I can't think of a simple solution, maybe create a wrapper setting the name of the thread (how?) or adding the session id to the thread and retrieving it? Cheers, Renato
|
Renato Losio - www.arsenio.it - renatoweb@arsenio.it
|
 |
Rusty Smythe
Ranch Hand
Joined: Aug 09, 2006
Posts: 93
|
|
Do you think that Session ID will contain useful information that you can use to identify a user? (I'm guessing... no) What if you expanded your methods to include a parameter of 'String username' and then passed the username from the controller? Then you could prepend/append the username to the log text. If you do this, just make sure that you handle the case where the session expires and your username may be null/undefined.
|
 |
Renato Losio
Ranch Hand
Joined: Nov 23, 2005
Posts: 99
|
|
> Do you think that Session ID will contain useful > information that you can use to identify a user? (I'm guessing... no) Sorry, my fault. I don't care too much (well, that would be nice) to identify the user straight away, I just need to be to say which line of code are coming from the same user (or session) and which are not in a timespan of 2-3 seconds where I may have few hundreds lines... Renato
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
That thread id thing sounds real familiar. Maybe another project at work used it. It would be easy in my current system as it has a single controller servlet that could set it for all requests and the logger already prints threadid. I guess you don't have an easy single entry point to modify?
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Originally posted by Stan James: I guess you don't have an easy single entry point to modify?
With filters, we all have an easy entry point to modify.
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8212
|
|
|
MDC in log4j, is what you are looking for. There was this similar thread in the Other Open Source Projects forum.(I am assuming that you are using log4j for logging).
|
[My Blog] [JavaRanch Journal]
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
Neat, thanks for the link. It says MDC is a thread local. Threads go back into the pool between requests. Would you need something to re-initialize it on new requests? Maybe I just didn't read far enough.
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8212
|
|
Threads go back into the pool between requests. Would you need something to re-initialize it on new requests?
I would not worry about re-initializing because, we are trying to track the log messages *per request*. So irrespective of whether the threads are sent back to pool between requests, we would be populating the MDC on *every request*, may be in a servlet filter. Something like:
MyServletFilter.doFilter() { Principal user = httprequest.getUserPrincipal(); String strUser = user.getName(); MDC.put("username", strUser); }
So irrespective of whether the thread is the same or different one, i will be populating my MDC with the username so that this will be available as long as the request spans. I guess, your concern would be valid if the thread was sent back to pool when the *current request* is still being processed. These are just my thoughts with whatever little understanding i have about MDC. Feel free to correct me if i have got this wrong.
|
 |
Jaikiran Pai
Marshal
Joined: Jul 20, 2005
Posts: 8212
|
|
|
Stan James, your question appeared to be very valid, to me, when i read this article: Dont use ThreadLocal in Managed Environment
|
 |
 |
|
|
subject: Logging and session values
|
|
|