Use Sleuth trace-and span id in log4j2 patternlayout

medTech :

I want to use sleuth with log4j2. Therefor I used the .properties configuration. The problem is I can't get the sleuth parameters to the logs with my patternlayout. Can you tell me which pattern to use to get the sleuth parameters. In my case they are always empty.

What I did: Like mentioned here [%X{spanId}] and set spring.sleuth.log.slf4j.enabled=false

My whole pattern looks this way:

date: %d{ISO8601} loglevel: %X{loglevel} [%X{spanId}] user-agent: %X{useragent} servicename: %X{servicename} class:%X{classname} method:%X{methode} Line: %L message: %m%n

To insert the data I use the ThreadContext Object:

ThreadContext.put("trace", logdata.getTrace());

When I insert it this way I get the following for example:

NoopSpan(91406b61b7d23455/05657a824bc5c9e2)

But I want to have the data split up so i can use it with the elk stack

EDIT: When I use

System.out.prinln(MDC.get("X-B3_TraceId"))

in my controller class I get rhe right output. But when I use in my pattern %X{X-B3_TraceId} the output is null.

UPDATE: %X{X-B3_TraceId} works. I've had an issue with my asynchronous logging workflow.

Jacob :

try with this pattern :

 <property name="CONSOLE_LOG_PATTERN"
          value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327002&siteId=1