java - Set MDC variable in the main thread -
I have to log the constant value on every log line but the problem with the value is that the only way to get it is to code (I need calls and APIs first to get value). That's why I want to start the value and set it in the main thread, so that each new thread has already set the MDC reference with that value.
Currently I'm using Spring-Boot, so I tried to set it up with an App Listener:
@bean Public ApplicationsListener & lt; ContextRefreshedEvent & gt; SetLoggingContext () {Return New ApplicationListener & LT; ContextRefreshedEvent & gt; () {@Override Public Zero onApplicationEvent (ContextRefreshedEvent Event) {MDC.put ("InstanceID", "-value"); LOGGER.debug ("Setting the instance ID"); }}; }
This code generates this log:
12: 18: 59.096 [local host-startStop-1] default information osjeaAnnotationMBeanExporter - Registration JMX input For Sale at Startup 12: 18: 59.171 [Local Host-startStop-1] Default-Value Debug cmconfig.CommonWebConfiguration - Example ID Set 12: 18: 59.181 [Local Host-startStop-1] Default-Value Information osboot.SpringApplication - Start up ProtocolHandler ["http-Bio 8081"] 12 - 59,193 [Main] Default Info oacoyote.http11.Http11Protocol :: 18: 59.205 [Main] Default Information Org 18: 5.917 Ekand (running JVM 15.429) Application 12. Apache.coyote.ajp.AjpProtocol - Starting ProtocolHandler ["AJP-Bio 8009"] 12: 18: 59.207 [Main] default information org.apache.catalina.startup.Catalina - Server 14,291 MS
As you can see, the value is set in a thread named
localhost-startStop-1
, so the main thread is no longer set. Well I want to set up MDC in the main context but I can not find a way to reach the main pouch, which is accompanied by walking inside the spring container.I know that I have% contextName of the logback Can achieve the same thing with the variable, but I want to continue using the interface of slf4j without using anything from one of the implementations.
Comments
Post a Comment