3

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.

FishingIsLife
  • 1,972
  • 3
  • 28
  • 51

1 Answers1

1

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}"/>
يعقوب
  • 1,008
  • 13
  • 14
  • when I use appender.error_file.layout.type = PatternLayout and appender.error_file.layout.pattern = the pattern you mention then I get the following:%clr(2019-09-27 08:00:55.375){faint} %clr(ERROR) %clr( ){magenta} %clr(---){faint} %clr([pool-2-thread-1]){faint} %clr(error_logger ){cyan} %clr(:){faint} This is an error log. – FishingIsLife Sep 27 '19 at 06:05
  • 1
    In general I just need to know if the trace and the span id are in the ThreadContext and how I can integrate them in my logging pattern – FishingIsLife Sep 27 '19 at 06:07