Reputation: 314
I am trying to implement dynamic debug logging by populating a ThreadContext
with specific attributes and filtering logs based on those attributes
.
Here’s what I have achieved so far:
Within the same JVM:
contextWrite()
method and cleared after the chain is completed.ExecutorService
, I wrap the Callable
in a ContextExecutorService
to manage the ThreadContext.Issue across JVMs:
In a distributed system (e.g., Hazelcast), where a process is submitted to another JVM, the context must be manually passed in a field and set in the thread context of the target JVM
. example process that is submitted to another JVM :
@Slf4j
public class LoggingEntryProcessor<K, V, R> implements EntryProcessor<K, V, R>, HazelcastInstanceAware {
private final EntryProcessor<K, V, R> processor;
private Map<String, String> attributes;
@Override
public R process(Entry<K, V> entry) {
// Ensure thread context is populated with attributes received from caller JVM and cleared after work is done
return processor.process(entry);
//the process method is nonreactive and may have reactive code inside of it.
}
private ContextSnapshot setContextSnapshot(Map<String, String> attributes) {
// tried creating a snapshot with which i can wrap, Slf4jThreadLocalAccessor is registered in ContextRegistry in this JVM
new Slf4jThreadLocalAccessor().setValue(attributes);
return ContextSnapshotFactory.builder().build().captureAll();
}
}
I tried to wrap the process call like this:
try (Scope scope = contextSnapshot.setThreadLocals()) {
return processor.process(entry);
}
Problems
Questions
Any insights or examples would be greatly appreciated!
Upvotes: 1
Views: 51
Reputation: 721
I'm not sure is this would be of help to you or not, but I had a similar issue I wanted to pass informaton to the logger but i have some information that goes through hazelcast and some other goes through ActiveMQ, I dont manipulate the ThreadContext myself but I rely on Slf4j MDC (https://www.slf4j.org/api/org/slf4j/MDC.html) as you can easily configure your logger to get information from it, this class has the methods getCopyOfContextMap()
and setContextMap(Map<String,String> contextMap)
so I just send a map as part of the message for ActiveMQ and I update it when it comes back so I have the same information on both sides on the MDC object once activemq returns.
Upvotes: 1