Your assumption is correct, but there is a way to avoid the problem. The issue 
is that, as one might expect, the ThreadContext is thread specific. So when the 
pattern is resolved it will be on the thread where the event is to be written, 
not the thread that called the logging method.

You can avoid this problem by not using the ThreadContext directly. Instead, 
get the context data out of the LogEvent. It will contain the ThreadContext 
data at the time the LogEvent was created. However, that would give you exactly 
the same thing %X does, so what is the point?

Ralph

> On Apr 9, 2021, at 9:00 AM, Lars-Fredrik Smedberg <itsme...@gmail.com> wrote:
> 
> Hi!
> 
> We are using different appenders with a pattern layout that both read
> values from ThreadContext using %X and through a custom lookup that
> internally access the ThreadContext
> 
> If we use a pattern such as "%X{testkey} $${m:testkey} %m" (the lookup
> assigned to prefix m access the same key from the ThreadContext) and then
> use our appender in an AsyncLogger then we get the correct value for the
> %X{testkey} but not for $${m:testkey}.
> 
> I assume the message is formatted on the caller thread but that the lookup
> part of the pattern is resolved on the async thread since it does not seem
> to find the key in the ThreadContext inside our lookup class?
> 
> Is the above the root cause to our problem? Is there a way around this? We
> really would like to use our custom lookup that use ThreadContext with
> async loggers.
> 
> Any help would be greatly apreciated
> Best regards
> LF
> 
> -- 
> Med vänlig hälsning / Best regards
> 
> Lars-Fredrik Smedberg
> 
> STATEMENT OF CONFIDENTIALITY:
> The information contained in this electronic message and any
> attachments to this message are intended for the exclusive use of the
> address(es) and may contain confidential or privileged information. If
> you are not the intended recipient, please notify Lars-Fredrik Smedberg
> immediately at itsme...@gmail.com, and destroy all copies of this
> message and any attachments.



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

Reply via email to