Closed fs-aikito closed 3 years ago
But what if you are not using slf4j? Is slf4j always a requirement for logback? I don't want to add the dependency if not.
Yes, slf4j-api is already pulled as a dependency for logback-classic:
https://github.com/qos-ch/logback/blob/master/logback-classic/pom.xml#L25
I actually think that this is a more general problem. Folks could be reusing the event objects causing race conditions. I'm going to add a copy events boolean which defaults to true.
I think this has been fixed also by https://github.com/j256/cloudwatch-logback-appender/pull/31
I'm using logstash/logstash-logback-encoder with cloudwatch-logback-appender in a REST API service and in some instances the log events are missing all MDC entries.
My guess what is happening that the log event has a reference to the MDC, and I'm clearing the MDC at the end of my request processing[1]. If the event sits in the appender internal queue too long, it will read the MDC data after it has been cleared.
If that is the case then one solution would be to store a copy of MDC contents to the LoggingEvent in the synchronous path, similarly as thread name is handled here:
https://github.com/j256/cloudwatch-logback-appender/blob/master/src/main/java/com/j256/cloudwatchlogbackappender/CloudWatchAppender.java#L179
[1] See the logback MDC servlet filter for an example: https://logback.qos.ch/xref/ch/qos/logback/classic/helpers/MDCInsertingServletFilter.html