rfoltyns / log4j2-elasticsearch

Log4j2 Elasticsearch Appender plugins
Apache License 2.0
174 stars 46 forks source link

How to pass the value from ThreadContext to ValueProperty? #58

Closed vaomaohaoone closed 3 years ago

vaomaohaoone commented 3 years ago

Hi! I tried passing value from ThreadContext to VirtualProperty like this:

my code in Application: ThreadContext.put("account", "elvisPresley"); Logger logger = LogManager.getLogger("elasticsearch"); logger.info("HELLO");

section with ValueProperty in log4j2.xml: `

`

And I keep getting 'undefined' in Kibana fot my field. Does anyone know how to solve this? It is my mistake or a bug?

vaomaohaoone commented 3 years ago

I think, that a problem with threads (main thread and AsyncLogger thread are different :) )

rfoltyns commented 3 years ago

Yes, you're right - with AsyncLogger log serialization is happening on Log4j2 AsyncLoggerConfig thread, not the logging thread.

The difference in context data handling with Logger and AsyncLogger is that the values are not accessible via ctx lookup.

However, context data is still copied to AsyncLogger's RingBufferLogEvent and MutableLogEvent and resolved on the fly, so you can serialize it with your own, custom serializer for context data as described in this comment

I hope it helps.

vaomaohaoone commented 3 years ago

Thanks!