Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

What's is idiomatic way of setting message-specific properties in log4j2?

What's is idiomatic way of setting message-specific properties in log4j2 in multi-threading application ?

My server allows external developers to create plugins for it. These plugins would be used for periodic jobs which represented by (reactive streams) Flux<> class.

I'd like my service to allow plugin developers to log events. But I want to define context-specific properties to these jobs (e.g. JobID). There could be multiple simultenious jobs running at the same time.

Typically people use log4j's MDC/ThreadContext for this. But because server is built using reactive streams different steps of "job" can be executed in different threads.

At the moment I've implemented very hacky workaround. I dictate plugin developers to use

interface TelemetryFactory {
    fun getLogger(clazz: Class<*>): Logger
    fun getLogger(name: String): Logger
}

to create instance of the Logger. In implementation of this interface I wrap MessageFactory2.newMessage() calls by calling ThreadContext.putAll() with my context-specific fields.

class MessageFactory2Wrapper(private val fields: Map<String, String>): MessageFactory2 {
   ...
}

This works only because I see in source-code of log4j that default message factory is ReusableMessageFactory therefore message and thread-context will reach appenders before subsequent logger call will change ThreadContext.

I wish org.apache.logging.log4j.message.Message had some kind of Map<String, String> that I can use in loging layout.

What should I do ?

question from:https://stackoverflow.com/questions/66049925/whats-is-idiomatic-way-of-setting-message-specific-properties-in-log4j2

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...