• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Logging and session values

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 93
Mac Objective C Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> 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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stan James, your question appeared to be very valid, to me, when i read this article: Dont use ThreadLocal in Managed Environment
 
Normally trees don't drive trucks. Does this tiny ad have a license?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic