Each concurrent http request will be served by seperate threads, however,
when a request is finished, the same thread may be reused to serve
additional requests.  This is normal in a webserver.

If you set up a request listener or filter, it will be called at the start
of every request.  In this listener, you can set an id.  Depending on the
technology stack you are using there are different ways of accomplishing
this.
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequestListener.html
Ex:
public void requestInitialized(ServletRequestEvent sre){
   MDC.put( "userId", new UUID() );
}


or
https://docs.oracle.com/javaee/6/api/javax/servlet/Filter.html

public void doFilter(...){
   MDC.put( "userId", new UUID() );
   MDC.put( ... ... );
   chain.doFilter(args);
}

This UUID can then be accessed in your logging statement automatically.  In
your code, you would use normal logging statements: logger.info("some log
info").

Your Appender would be configured with the particular parameter in its
string.  Ex:
<param name="ConversionPattern" value="RequestUUID: %X{userId} %d{ISO8601}
%-5p %c{1} - %m%n" />

Then your logger would automatically printout your log as something like:
RequestUUID: 12afe1938107 2015-11-04 11:02:03  some.class.Name:159 - some
log info


Since the MDC var is overwritten at the start of every request, you can be
assured that the thread will contain the appropriate value for that
request.  And since all concurrent requests are handled by independent
threads, there will be no overlapping.

You should be able to find plenty of resources/tutorials for ways to use
MDC online (stackoverflow or other).  Here's a quick primer:
http://veerasundar.com/blog/2009/10/log4j-mdc-mapped-diagnostic-context-what-and-why/

Hope this clarifies things a little.  Let me know if anything is still
unclear.

Thanks,

Eric



On Thu, Nov 5, 2015 at 10:59 AM, Anto Aravinth <[email protected]>
wrote:

> Thanks Eric, thats exactly what I was looking for.
>
> A noob question, so if a user hits the webpage, servlet is going to create
> a thread for the same. Now if I do logger after setting up MDC id like this:
>
>    logger.info (" Great")
>
> So technically how this can be converted or remembered per thread?
>
> Because imagine I did set the id as 121, and two users are hitting, which
> is going to be altogether different threads, how does this 121 gets binded
> to threads in this case?
>
> From my understanding so if a user going to access the webpage, then he or
> she needs to "somehow" pass on this id to MDC, then I believe it works as
> required.
>
> Correct me if I'm wrong.
>
> Anto.
> On 5 Nov 2015 16:10, "Eric B" <[email protected]> wrote:
>
>> What logging platform are you using?
>>
>> If you are using Log4j, you can use the MappedDiagnosticContext (MDC)
>> class
>> https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html.
>> This is managed on a per-thread basis, so you can assign an ID in the MDC
>> as part of a request listener, and any Log4j call that is made in that
>> thread will have access to the id stored in the MDC.  This allows you to
>> recall the id in a logging statement.
>>
>> All logs will still be pushed to the same appender, however you would
>> then be able to sort and/or filter based on the ID.  If memory serves,
>> retrieving the variable for the appender is done via the %X{VarName}.
>>
>> I also believe that Logback has a similar MDC functionality.
>>
>> Thanks,
>>
>> Eric
>>
>>
>>
>> On Thu, Nov 5, 2015 at 9:34 AM, ants <[email protected]> wrote:
>>
>>> I have few questions on this specific task; Now I have written by
>>> monitoring
>>> logic in the aspectj. It works so smoothly and tested in tomcat and other
>>> servers as well.
>>>
>>> I was wondering, if this can be done as well:
>>>
>>> 1. The logging gets started when I turn on few params on my aspectj jar,
>>> which starts logging.
>>> 2. So if possible, when it logs, can I make it to log according to each
>>> thread level?
>>>
>>> What I was thinking over here is this, I will start my logging param in
>>> aspectj, so imagine, when two users access the webpage in the server,
>>> now I
>>> want the trace should be completely isolated from each of the request. I
>>> know HTTP doesn't have states, we can do this in sort of attaching some
>>> "sessionID" to each trace, and then do a batch operation for getting the
>>> result of each thread; so that trace "flow" is perfect.
>>>
>>> I just want to know if there are any other ways of doing this?
>>>
>>> Your thoughts will be really helpful to me.
>>>
>>>
>>>
>>> --
>>> View this message in context:
>>> http://aspectj.2085585.n4.nabble.com/How-to-weave-the-method-calls-inside-an-war-file-tp4651913p4651968.html
>>> Sent from the AspectJ - users mailing list archive at Nabble.com.
>>> _______________________________________________
>>> aspectj-users mailing list
>>> [email protected]
>>> To change your delivery options, retrieve your password, or unsubscribe
>>> from this list, visit
>>> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>>>
>>
>>
>> _______________________________________________
>> aspectj-users mailing list
>> [email protected]
>> To change your delivery options, retrieve your password, or unsubscribe
>> from this list, visit
>> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>>
>
> _______________________________________________
> aspectj-users mailing list
> [email protected]
> To change your delivery options, retrieve your password, or unsubscribe
> from this list, visit
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
[email protected]
To change your delivery options, retrieve your password, or unsubscribe from 
this list, visit
https://dev.eclipse.org/mailman/listinfo/aspectj-users

Reply via email to