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 Lars-Fredrik Smedberg <it...@gmail.com> on 2021/04/09 16:00:46 UTC

Question on accessing the threadcontext in pattern vs lookup

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 itsmeden@gmail.com, and destroy all copies of this
message and any attachments.

Re: Question on accessing the threadcontext in pattern vs lookup

Posted by Volkan Yazıcı <vo...@gmail.com>.
AFAIC, your reasoning on why m:testkey not working is correct. Why don't
you create your own PatternConverter rather than using a custom lookup?

Additionally, if your log sink accepts JSON, you can introduce a custom
resolver to JsonTemplateLayout too -- though note that this feature will be
available with the upcoming 2.5.0 release.

On Fri, 9 Apr 2021, 18:01 Lars-Fredrik Smedberg <it...@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 itsmeden@gmail.com, and destroy all copies of this
> message and any attachments.
>

Re: Question on accessing the threadcontext in pattern vs lookup

Posted by Lars-Fredrik Smedberg <it...@gmail.com>.
Hi

Thanks all for the answers.

I actually did some debugging and changed to the thread context data on the
log event around the time for the first reply (when i saw that the log
event was constructed before passing it to the async logger). The reason
for using the lookup and not only %X is that is does more than just pass
the value through (at least for some keys). The lookup also provides values
not based upon the thread context so it's also for consistency reasons
towards the internal users/developers.

Preciate everyone's time, thanks
Best regards
LF

On Sat, Apr 10, 2021, 22:24 Ralph Goers <ra...@dslextreme.com> wrote:

> 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 <it...@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 itsmeden@gmail.com, and destroy all copies of this
> > message and any attachments.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Re: Question on accessing the threadcontext in pattern vs lookup

Posted by Ralph Goers <ra...@dslextreme.com>.
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 <it...@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 itsmeden@gmail.com, and destroy all copies of this
> message and any attachments.



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