[
https://issues.apache.org/jira/browse/LOG4J2-2855?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17116991#comment-17116991
]
Ralph Goers commented on LOG4J2-2855:
-------------------------------------
IMO, There is no auto-magic answer for dealing with Thread pools. First, you
can't use an InheritalbeThreadLocal most of the time because frequently the
pools are created on one main thread while the workers are created on a
different thread. This is the case for Log4j's Scheduler Thread pool and
Tomcat's dispatcher thread pool.
The only way to do it is to capture the ThreadContext Map in the "owning
thread", pass the Map to the worker thread and have the worker recreated the
ThreadContext Map before the worker class gets called. While Log4j could
provide something to do this we don't want to get into the business of Thread
pool management.
> Managing ThreadContext across thread pool and worker threads
> ------------------------------------------------------------
>
> Key: LOG4J2-2855
> URL: https://issues.apache.org/jira/browse/LOG4J2-2855
> Project: Log4j 2
> Issue Type: Question
> Components: API
> Affects Versions: 2.10.0
> Reporter: Peter Podniesinski
> Priority: Minor
>
> Hello, my team supports a spring boot rest API and we have been leveraging
> _ThreadContext_ to share meta-data about our api requests to the log events
> we record throughout our request processing.
> Recently we had noticed that logs generated from thread-pool or worker
> threads *do not* share this context and the contextMap would be empty.
> For that reason we enabled
> {code:java}
> isThreadContextMapInheritable=true
> {code}
> This indeed shares the context to child threads, but now we are having an
> issue where the contextMap is bleeding into the logs of other API requests.
> Please advise how to better tune this functionality.
> Below is a snippet of code where we build the ThreadContext in the API
> resource and attempt to clear it before another request comes in.
> {code:java}
> try {
> ThreadContext.put("id", request.getId());
> <..more context..>
> return evaluate(request
> } finally { ThreadContext.clearAll(); }
> {code}
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)