vy commented on pull request #472:
URL: https://github.com/apache/logging-log4j2/pull/472#issuecomment-791330965


   So, I guess, both of us are right.
   
   1. Yes, `JsonTemplateLayout` can serialize any object type, not only 
strings. `JTL` accesses to the thread context data using 
`LogEvent#getContextData()` method of return type `ReadOnlyStringMap`. There it 
uses the following method of the `ROSM`: `<V, S> void forEach(final 
TriConsumer<String, ? super V, S> action, final S state)`. See that here the 
`value` can be any `Object`. Check out MDC tests in `JsonTemplateLayoutTest` to 
see how there we test serialization for different types in addition to strings.
   2. Yes, you cannot _easily_ put anything into MDC using `ThreadContext` 
class provided methods.
   
   Note my _"easily"_ remark. I think you can do the following to put 
non-String values into MDC:
   
   ```java
   ObjectThreadContextMap contextMap = (ObjectThreadContextMap) 
ThreadContext.getThreadContextMap();
   contextMap.putValue("string", "foo");
   contextMap.putValue("integer", 1);
   contextMap.putValue("map", Collections.singletonMap("foo", "bar"));
   contextMap.putValue("array", new Object[]{"foo", 0xDEADBEEF});
   ```
   
   Though this will only work if `Constants.ENABLE_THREADLOCALS` is `true`:
   
   ```
   boolean IS_WEB_APP = PropertiesUtil
           .getProperties()
           .getBooleanProperty("log4j2.is.webapp", 
isClassAvailable("javax.servlet.Servlet"));
   
   boolean ENABLE_THREADLOCALS = !IS_WEB_APP &&
           PropertiesUtil
                   .getProperties()
                   .getBooleanProperty("log4j2.enable.threadlocals", true);
   ```
   
   **Would you mind checking if this works for you?** In the meantime, I will 
raise this shortcoming of `ThreadContext` in the Log4j `dev` mailing list.


----------------------------------------------------------------
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.

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


Reply via email to