[ 
https://issues.apache.org/jira/browse/LOG4J2-1010?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Remko Popma updated LOG4J2-1010:
--------------------------------
    Summary: Possibility to set ThreadContext values in calls to Logger method  
(was: Posibility to set ThreadContext values in calls to Logger metod)

Let me quickly mention the use case for this request from your explanation in 
the mailing list.

bq. I have an asynchronous web app (in Scala based on Twitter Finagle, not 
using Servlet API). I use Log4j 2.2 for logging, and I want to tag log messages 
with some information about requests (such as client IP address and 
authenticated user ID). In a synchronous Servlet based web app, you can use 
Log4j's ThreadContext for this, but it doesn't work in Finagle's asynchronous 
environment since there is no correlation between threads and requests. How can 
I do this?

One idea would be to create a custom 
[MessageFactory|http://logging.apache.org/log4j/2.x/log4j-api/xref/org/apache/logging/log4j/message/MessageFactory.html].
 This class would be aware of the web container and would be able to get the 
correct client IP address and userID values, and set these values in each 
Message it creates. In your custom Message implementation, you would then 
include these values in the return value of the 
[Message.getFormattedMessage()|http://logging.apache.org/log4j/2.x/log4j-api/xref/org/apache/logging/log4j/message/Message.html#L47]
 method. 

Your application would obtain {{Logger}} instances by calling one of the 
{{LogManager.getLogger(..., MessageFactory)}} methods.

The drawback is that once these values are part of the Message, you will not be 
able to suppress them by configuring a different Layout, so this is not as 
flexible as ThreadContext. (But this idea works, and ThreadContext doesn't... 
:-) )

> Possibility to set ThreadContext values in calls to Logger method
> -----------------------------------------------------------------
>
>                 Key: LOG4J2-1010
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1010
>             Project: Log4j 2
>          Issue Type: Improvement
>          Components: API
>    Affects Versions: 2.2
>            Reporter: Mikael Ståldal
>
> It would be useful to have some logging methods in the Logger interface to 
> set ThreadContext values for a single log message only.
> In an asynchronous environment, using ThreadContext as currently defined is 
> not so useful since JVM threads might not be coupled to the logical flow of 
> the application.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to