ppkarwasz commented on PR #2419:
URL: https://github.com/apache/logging-log4j2/pull/2419#issuecomment-2028616139

   @rgoers,
   
   > 1. We cannot reuse `ThreadContextMap` without breaking it. It only allows 
String values. Relaxing that allows users to store objects, which we know 
causes problems. Due to the way `ScopedContext` behaves storing objects is not 
a problem.
   
   I think that we can reuse `ThreadContextMap`. Since Log4j API 2.8 there is a 
(very dangerous) 
[`ObjectThreadContextMap`](https://logging.apache.org/log4j/2.x/javadoc/log4j-api/org/apache/logging/log4j/spi/ObjectThreadContextMap)
 interface for which we have one implementation.
   
   While I **totally** agree that exposing the functionality of that interface 
through `ThreadContext` would be simply insane, exposing it through 
`ScopedContext` is perfectly **safe**: your implementation guarantees that all 
`Object` values that a user might insert into the thread context will be 
removed when your `run` method ends.
   
   Sure, implementing `ScopedContext` using `ThreadContextMap` is a little bit 
harder, since the former is `Deque`-based and the latter has a single value, 
but as I remarked in [this comment on 
#2330](https://github.com/apache/logging-log4j2/pull/2330#pullrequestreview-1924545044)
 we might need to add two static methods somewhere in `o.a.l.l.spi` that allow 
to rapidly take a snapshot of a context and restore it:
   
   ```java
       /**
        * Saves the current context map data.
        * <p>
        *     The returned object is not affected by later changes to the 
context map.
        * </p>
        * @return An opaque thread-safe version of the internal context map 
representation.
        * @see #restoreContextMap
        * @since 2.24
        */
       static Object saveContextMap();
   
       /**
        * Restores the context map data, from a saved version.
        * @param contextMap An opaque thread-safe version of the internal 
context map representation.
        * @see #saveContextMap
        * @since 2.24
        */
       static void restoreContextMap(Object contextMap);
   ```
   
   These methods might be useful in `CloseableThreadContext` and for 
integrators to more easily transfer the thread context from one thread to 
another, without converting it from and to a `Map<String, String>`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to