You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Kevin Jung <my...@gmail.com> on 2018/04/17 02:01:08 UTC

Need to clear ThreadContext for each HTTP request?

Hello,

I am using Log4j 2 ThreadContext with Tomcat.  I wonder if I need to clear
ThreadContext at the end (or start) of HTTP request in order to avoid the
context data being carried over to next HTTP request processing.

I was assuming that thread context is cleared when Tomcat thread is put
back in the thread pool and when a Tomcat thread starts processing new HTTP
request, it starts with an empty thread context, nothing carried over from
the previous HTTP request.  But that's my assumption, I want to make sure I
do not need to clear at the end, like using a servlet filter.  Or should I
use CloseableThreadContext to avoid carrying over among HTTP requests?

Thanks,
Kevin

Re: Need to clear ThreadContext for each HTTP request?

Posted by Remko Popma <re...@gmail.com>.
Very good question. Please be aware that Log4j will *not* clear the ThreadContext. Your concern that ThreadContext values may become visible to other threads because of Tomcat’s thread pool is a legitimate concern. 

It is a good idea to explicitly clear values when the application is done with them. The CloseableThreadContext can be useful here. 

Remko

(Shameless plug) Every java main() method deserves http://picocli.info

> On Apr 17, 2018, at 11:01, Kevin Jung <my...@gmail.com> wrote:
> 
> Hello,
> 
> I am using Log4j 2 ThreadContext with Tomcat.  I wonder if I need to clear
> ThreadContext at the end (or start) of HTTP request in order to avoid the
> context data being carried over to next HTTP request processing.
> 
> I was assuming that thread context is cleared when Tomcat thread is put
> back in the thread pool and when a Tomcat thread starts processing new HTTP
> request, it starts with an empty thread context, nothing carried over from
> the previous HTTP request.  But that's my assumption, I want to make sure I
> do not need to clear at the end, like using a servlet filter.  Or should I
> use CloseableThreadContext to avoid carrying over among HTTP requests?
> 
> Thanks,
> Kevin

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Need to clear ThreadContext for each HTTP request?

Posted by Kevin Jung <my...@gmail.com>.
Agreed.  I don't think we need a separate filter from log4j2.  People may
already have filters and just need to add one line there; or can create one
easily if they don't have.

But I think it's good to mention somewhere, probably on
https://logging.apache.org/log4j/2.x/manual/thread-context.html that thread
context needs to be cleared to avoid being carried over to the next run
when thread context is used in a thread pool, e.g. Tomcat thread pool or
Camel thread pool.  If the document gives a sample filter, it would be more
helpful.

On Wed, Apr 18, 2018 at 3:30 PM, Remko Popma <re...@gmail.com> wrote:

> This is not something that can or should be automated imho. ThreadContext
> data disappearing automatically can be just as confusing to troubleshoot
> (or more so).
>
> Remko
>
> > On Apr 19, 2018, at 2:40, Gary Gregory <ga...@gmail.com> wrote:
> >
> > On Tue, Apr 17, 2018 at 10:22 AM, Ralph Goers <
> ralph.goers@dslextreme.com>
> > wrote:
> >
> >> I have mixed feelings about that. Every filter I have created that
> clears
> >> the ThreadContext at the end also injects values into the ThreadContext
> at
> >> the beginning of the request, just as the example at
> >> http://logging.apache.org/log4j/2.x/manual/eventlogging.html <
> >> http://logging.apache.org/log4j/2.x/manual/eventlogging.html> does. So
> if
> >> you were going to create a filter you would also want to provide a
> method
> >> to do the setup, so your filter code would look something like:
> >>
> >> public void doFilter(ServletRequest servletRequest, ServletResponse
> >> servletResponse, FilterChain filterChain throws IOException,
> >> ServletException {
> >>        setupRequest(servletRequest, servletResponse);
> >>        filterChain.doFilter(servletRequest, servletResponse);
> >>        ThreadContext.clear();
> >> }
> >>
> >> public void setupRequest(servletRequest, servletResponse) {
> >> }
> >>
> >> But the only value this really has is the one line that was added to
> clear
> >> the ThreadContext.  Is that really much of a “convenience”?
> >>
> >
> > If that saves me the 30 minutes to Google, filter, understand, copy, and
> > paste, then yes it is worth it.
> >
> > Gary
> >
> >
> >>
> >> Ralph
> >>
> >>
> >>> On Apr 17, 2018, at 6:29 AM, Gary Gregory <ga...@gmail.com>
> >> wrote:
> >>>
> >>> Should we provide a stock Log4j filter that does that? A nice
> >> convenience.
> >>>
> >>> Gary
> >>>
> >>> On Mon, Apr 16, 2018 at 10:01 PM, Kevin Jung <my...@gmail.com>
> >> wrote:
> >>>
> >>>> Thanks Remko and Ralph for the information.  I thought of clearing in
> a
> >>>> filter, but somehow I thought there might be a clean up mechanism
> >> already
> >>>> because using ThreadContext in Tomcat is a quite common thing.  But
> that
> >>>> was just my assumption.  I agree that it's better to *clear
> explicitly*.
> >>>>
> >>>> Thanks,
> >>>> Kevin
> >>>>
> >>>> On Mon, Apr 16, 2018 at 8:32 PM, Ralph Goers <
> >> ralph.goers@dslextreme.com>
> >>>> wrote:
> >>>>
> >>>>> See the example at http://logging.apache.org/
> >>>>> log4j/2.x/manual/eventlogging.html <http://logging.apache.org/
> >>>>> log4j/2.x/manual/eventlogging.html>. You will see the call to clear
> >> the
> >>>>> ThreadContext. It does indeed use a filter.
> >>>>>
> >>>>> Ralph
> >>>>>
> >>>>>> On Apr 16, 2018, at 7:01 PM, Kevin Jung <my...@gmail.com>
> >> wrote:
> >>>>>>
> >>>>>> Hello,
> >>>>>>
> >>>>>> I am using Log4j 2 ThreadContext with Tomcat.  I wonder if I need to
> >>>>> clear
> >>>>>> ThreadContext at the end (or start) of HTTP request in order to
> avoid
> >>>> the
> >>>>>> context data being carried over to next HTTP request processing.
> >>>>>>
> >>>>>> I was assuming that thread context is cleared when Tomcat thread is
> >> put
> >>>>>> back in the thread pool and when a Tomcat thread starts processing
> new
> >>>>> HTTP
> >>>>>> request, it starts with an empty thread context, nothing carried
> over
> >>>>> from
> >>>>>> the previous HTTP request.  But that's my assumption, I want to make
> >>>>> sure I
> >>>>>> do not need to clear at the end, like using a servlet filter.  Or
> >>>> should
> >>>>> I
> >>>>>> use CloseableThreadContext to avoid carrying over among HTTP
> requests?
> >>>>>>
> >>>>>> Thanks,
> >>>>>> Kevin
> >>>>>
> >>>>>
> >>>>
> >>
> >>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: Need to clear ThreadContext for each HTTP request?

Posted by Remko Popma <re...@gmail.com>.
This is not something that can or should be automated imho. ThreadContext data disappearing automatically can be just as confusing to troubleshoot (or more so). 

Remko

> On Apr 19, 2018, at 2:40, Gary Gregory <ga...@gmail.com> wrote:
> 
> On Tue, Apr 17, 2018 at 10:22 AM, Ralph Goers <ra...@dslextreme.com>
> wrote:
> 
>> I have mixed feelings about that. Every filter I have created that clears
>> the ThreadContext at the end also injects values into the ThreadContext at
>> the beginning of the request, just as the example at
>> http://logging.apache.org/log4j/2.x/manual/eventlogging.html <
>> http://logging.apache.org/log4j/2.x/manual/eventlogging.html> does. So if
>> you were going to create a filter you would also want to provide a method
>> to do the setup, so your filter code would look something like:
>> 
>> public void doFilter(ServletRequest servletRequest, ServletResponse
>> servletResponse, FilterChain filterChain throws IOException,
>> ServletException {
>>        setupRequest(servletRequest, servletResponse);
>>        filterChain.doFilter(servletRequest, servletResponse);
>>        ThreadContext.clear();
>> }
>> 
>> public void setupRequest(servletRequest, servletResponse) {
>> }
>> 
>> But the only value this really has is the one line that was added to clear
>> the ThreadContext.  Is that really much of a “convenience”?
>> 
> 
> If that saves me the 30 minutes to Google, filter, understand, copy, and
> paste, then yes it is worth it.
> 
> Gary
> 
> 
>> 
>> Ralph
>> 
>> 
>>> On Apr 17, 2018, at 6:29 AM, Gary Gregory <ga...@gmail.com>
>> wrote:
>>> 
>>> Should we provide a stock Log4j filter that does that? A nice
>> convenience.
>>> 
>>> Gary
>>> 
>>> On Mon, Apr 16, 2018 at 10:01 PM, Kevin Jung <my...@gmail.com>
>> wrote:
>>> 
>>>> Thanks Remko and Ralph for the information.  I thought of clearing in a
>>>> filter, but somehow I thought there might be a clean up mechanism
>> already
>>>> because using ThreadContext in Tomcat is a quite common thing.  But that
>>>> was just my assumption.  I agree that it's better to *clear explicitly*.
>>>> 
>>>> Thanks,
>>>> Kevin
>>>> 
>>>> On Mon, Apr 16, 2018 at 8:32 PM, Ralph Goers <
>> ralph.goers@dslextreme.com>
>>>> wrote:
>>>> 
>>>>> See the example at http://logging.apache.org/
>>>>> log4j/2.x/manual/eventlogging.html <http://logging.apache.org/
>>>>> log4j/2.x/manual/eventlogging.html>. You will see the call to clear
>> the
>>>>> ThreadContext. It does indeed use a filter.
>>>>> 
>>>>> Ralph
>>>>> 
>>>>>> On Apr 16, 2018, at 7:01 PM, Kevin Jung <my...@gmail.com>
>> wrote:
>>>>>> 
>>>>>> Hello,
>>>>>> 
>>>>>> I am using Log4j 2 ThreadContext with Tomcat.  I wonder if I need to
>>>>> clear
>>>>>> ThreadContext at the end (or start) of HTTP request in order to avoid
>>>> the
>>>>>> context data being carried over to next HTTP request processing.
>>>>>> 
>>>>>> I was assuming that thread context is cleared when Tomcat thread is
>> put
>>>>>> back in the thread pool and when a Tomcat thread starts processing new
>>>>> HTTP
>>>>>> request, it starts with an empty thread context, nothing carried over
>>>>> from
>>>>>> the previous HTTP request.  But that's my assumption, I want to make
>>>>> sure I
>>>>>> do not need to clear at the end, like using a servlet filter.  Or
>>>> should
>>>>> I
>>>>>> use CloseableThreadContext to avoid carrying over among HTTP requests?
>>>>>> 
>>>>>> Thanks,
>>>>>> Kevin
>>>>> 
>>>>> 
>>>> 
>> 
>> 

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Need to clear ThreadContext for each HTTP request?

Posted by Gary Gregory <ga...@gmail.com>.
On Tue, Apr 17, 2018 at 10:22 AM, Ralph Goers <ra...@dslextreme.com>
wrote:

> I have mixed feelings about that. Every filter I have created that clears
> the ThreadContext at the end also injects values into the ThreadContext at
> the beginning of the request, just as the example at
> http://logging.apache.org/log4j/2.x/manual/eventlogging.html <
> http://logging.apache.org/log4j/2.x/manual/eventlogging.html> does. So if
> you were going to create a filter you would also want to provide a method
> to do the setup, so your filter code would look something like:
>
> public void doFilter(ServletRequest servletRequest, ServletResponse
> servletResponse, FilterChain filterChain throws IOException,
> ServletException {
>         setupRequest(servletRequest, servletResponse);
>         filterChain.doFilter(servletRequest, servletResponse);
>         ThreadContext.clear();
> }
>
> public void setupRequest(servletRequest, servletResponse) {
> }
>
> But the only value this really has is the one line that was added to clear
> the ThreadContext.  Is that really much of a “convenience”?
>

If that saves me the 30 minutes to Google, filter, understand, copy, and
paste, then yes it is worth it.

Gary


>
> Ralph
>
>
> > On Apr 17, 2018, at 6:29 AM, Gary Gregory <ga...@gmail.com>
> wrote:
> >
> > Should we provide a stock Log4j filter that does that? A nice
> convenience.
> >
> > Gary
> >
> > On Mon, Apr 16, 2018 at 10:01 PM, Kevin Jung <my...@gmail.com>
> wrote:
> >
> >> Thanks Remko and Ralph for the information.  I thought of clearing in a
> >> filter, but somehow I thought there might be a clean up mechanism
> already
> >> because using ThreadContext in Tomcat is a quite common thing.  But that
> >> was just my assumption.  I agree that it's better to *clear explicitly*.
> >>
> >> Thanks,
> >> Kevin
> >>
> >> On Mon, Apr 16, 2018 at 8:32 PM, Ralph Goers <
> ralph.goers@dslextreme.com>
> >> wrote:
> >>
> >>> See the example at http://logging.apache.org/
> >>> log4j/2.x/manual/eventlogging.html <http://logging.apache.org/
> >>> log4j/2.x/manual/eventlogging.html>. You will see the call to clear
> the
> >>> ThreadContext. It does indeed use a filter.
> >>>
> >>> Ralph
> >>>
> >>>> On Apr 16, 2018, at 7:01 PM, Kevin Jung <my...@gmail.com>
> wrote:
> >>>>
> >>>> Hello,
> >>>>
> >>>> I am using Log4j 2 ThreadContext with Tomcat.  I wonder if I need to
> >>> clear
> >>>> ThreadContext at the end (or start) of HTTP request in order to avoid
> >> the
> >>>> context data being carried over to next HTTP request processing.
> >>>>
> >>>> I was assuming that thread context is cleared when Tomcat thread is
> put
> >>>> back in the thread pool and when a Tomcat thread starts processing new
> >>> HTTP
> >>>> request, it starts with an empty thread context, nothing carried over
> >>> from
> >>>> the previous HTTP request.  But that's my assumption, I want to make
> >>> sure I
> >>>> do not need to clear at the end, like using a servlet filter.  Or
> >> should
> >>> I
> >>>> use CloseableThreadContext to avoid carrying over among HTTP requests?
> >>>>
> >>>> Thanks,
> >>>> Kevin
> >>>
> >>>
> >>
>
>

Re: Need to clear ThreadContext for each HTTP request?

Posted by Ralph Goers <ra...@dslextreme.com>.
I have mixed feelings about that. Every filter I have created that clears the ThreadContext at the end also injects values into the ThreadContext at the beginning of the request, just as the example at http://logging.apache.org/log4j/2.x/manual/eventlogging.html <http://logging.apache.org/log4j/2.x/manual/eventlogging.html> does. So if you were going to create a filter you would also want to provide a method to do the setup, so your filter code would look something like:

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain throws IOException, ServletException {
	setupRequest(servletRequest, servletResponse);
	filterChain.doFilter(servletRequest, servletResponse);
	ThreadContext.clear();
}

public void setupRequest(servletRequest, servletResponse) {
}

But the only value this really has is the one line that was added to clear the ThreadContext.  Is that really much of a “convenience”?

Ralph


> On Apr 17, 2018, at 6:29 AM, Gary Gregory <ga...@gmail.com> wrote:
> 
> Should we provide a stock Log4j filter that does that? A nice convenience.
> 
> Gary
> 
> On Mon, Apr 16, 2018 at 10:01 PM, Kevin Jung <my...@gmail.com> wrote:
> 
>> Thanks Remko and Ralph for the information.  I thought of clearing in a
>> filter, but somehow I thought there might be a clean up mechanism already
>> because using ThreadContext in Tomcat is a quite common thing.  But that
>> was just my assumption.  I agree that it's better to *clear explicitly*.
>> 
>> Thanks,
>> Kevin
>> 
>> On Mon, Apr 16, 2018 at 8:32 PM, Ralph Goers <ra...@dslextreme.com>
>> wrote:
>> 
>>> See the example at http://logging.apache.org/
>>> log4j/2.x/manual/eventlogging.html <http://logging.apache.org/
>>> log4j/2.x/manual/eventlogging.html>. You will see the call to clear the
>>> ThreadContext. It does indeed use a filter.
>>> 
>>> Ralph
>>> 
>>>> On Apr 16, 2018, at 7:01 PM, Kevin Jung <my...@gmail.com> wrote:
>>>> 
>>>> Hello,
>>>> 
>>>> I am using Log4j 2 ThreadContext with Tomcat.  I wonder if I need to
>>> clear
>>>> ThreadContext at the end (or start) of HTTP request in order to avoid
>> the
>>>> context data being carried over to next HTTP request processing.
>>>> 
>>>> I was assuming that thread context is cleared when Tomcat thread is put
>>>> back in the thread pool and when a Tomcat thread starts processing new
>>> HTTP
>>>> request, it starts with an empty thread context, nothing carried over
>>> from
>>>> the previous HTTP request.  But that's my assumption, I want to make
>>> sure I
>>>> do not need to clear at the end, like using a servlet filter.  Or
>> should
>>> I
>>>> use CloseableThreadContext to avoid carrying over among HTTP requests?
>>>> 
>>>> Thanks,
>>>> Kevin
>>> 
>>> 
>> 


Re: Need to clear ThreadContext for each HTTP request?

Posted by Gary Gregory <ga...@gmail.com>.
Should we provide a stock Log4j filter that does that? A nice convenience.

Gary

On Mon, Apr 16, 2018 at 10:01 PM, Kevin Jung <my...@gmail.com> wrote:

> Thanks Remko and Ralph for the information.  I thought of clearing in a
> filter, but somehow I thought there might be a clean up mechanism already
> because using ThreadContext in Tomcat is a quite common thing.  But that
> was just my assumption.  I agree that it's better to *clear explicitly*.
>
> Thanks,
> Kevin
>
> On Mon, Apr 16, 2018 at 8:32 PM, Ralph Goers <ra...@dslextreme.com>
> wrote:
>
> > See the example at http://logging.apache.org/
> > log4j/2.x/manual/eventlogging.html <http://logging.apache.org/
> > log4j/2.x/manual/eventlogging.html>. You will see the call to clear the
> > ThreadContext. It does indeed use a filter.
> >
> > Ralph
> >
> > > On Apr 16, 2018, at 7:01 PM, Kevin Jung <my...@gmail.com> wrote:
> > >
> > > Hello,
> > >
> > > I am using Log4j 2 ThreadContext with Tomcat.  I wonder if I need to
> > clear
> > > ThreadContext at the end (or start) of HTTP request in order to avoid
> the
> > > context data being carried over to next HTTP request processing.
> > >
> > > I was assuming that thread context is cleared when Tomcat thread is put
> > > back in the thread pool and when a Tomcat thread starts processing new
> > HTTP
> > > request, it starts with an empty thread context, nothing carried over
> > from
> > > the previous HTTP request.  But that's my assumption, I want to make
> > sure I
> > > do not need to clear at the end, like using a servlet filter.  Or
> should
> > I
> > > use CloseableThreadContext to avoid carrying over among HTTP requests?
> > >
> > > Thanks,
> > > Kevin
> >
> >
>

Re: Need to clear ThreadContext for each HTTP request?

Posted by Kevin Jung <my...@gmail.com>.
Thanks Remko and Ralph for the information.  I thought of clearing in a
filter, but somehow I thought there might be a clean up mechanism already
because using ThreadContext in Tomcat is a quite common thing.  But that
was just my assumption.  I agree that it's better to *clear explicitly*.

Thanks,
Kevin

On Mon, Apr 16, 2018 at 8:32 PM, Ralph Goers <ra...@dslextreme.com>
wrote:

> See the example at http://logging.apache.org/
> log4j/2.x/manual/eventlogging.html <http://logging.apache.org/
> log4j/2.x/manual/eventlogging.html>. You will see the call to clear the
> ThreadContext. It does indeed use a filter.
>
> Ralph
>
> > On Apr 16, 2018, at 7:01 PM, Kevin Jung <my...@gmail.com> wrote:
> >
> > Hello,
> >
> > I am using Log4j 2 ThreadContext with Tomcat.  I wonder if I need to
> clear
> > ThreadContext at the end (or start) of HTTP request in order to avoid the
> > context data being carried over to next HTTP request processing.
> >
> > I was assuming that thread context is cleared when Tomcat thread is put
> > back in the thread pool and when a Tomcat thread starts processing new
> HTTP
> > request, it starts with an empty thread context, nothing carried over
> from
> > the previous HTTP request.  But that's my assumption, I want to make
> sure I
> > do not need to clear at the end, like using a servlet filter.  Or should
> I
> > use CloseableThreadContext to avoid carrying over among HTTP requests?
> >
> > Thanks,
> > Kevin
>
>

Re: Need to clear ThreadContext for each HTTP request?

Posted by Ralph Goers <ra...@dslextreme.com>.
See the example at http://logging.apache.org/log4j/2.x/manual/eventlogging.html <http://logging.apache.org/log4j/2.x/manual/eventlogging.html>. You will see the call to clear the ThreadContext. It does indeed use a filter.

Ralph

> On Apr 16, 2018, at 7:01 PM, Kevin Jung <my...@gmail.com> wrote:
> 
> Hello,
> 
> I am using Log4j 2 ThreadContext with Tomcat.  I wonder if I need to clear
> ThreadContext at the end (or start) of HTTP request in order to avoid the
> context data being carried over to next HTTP request processing.
> 
> I was assuming that thread context is cleared when Tomcat thread is put
> back in the thread pool and when a Tomcat thread starts processing new HTTP
> request, it starts with an empty thread context, nothing carried over from
> the previous HTTP request.  But that's my assumption, I want to make sure I
> do not need to clear at the end, like using a servlet filter.  Or should I
> use CloseableThreadContext to avoid carrying over among HTTP requests?
> 
> Thanks,
> Kevin