j256 / cloudwatch-logback-appender

Appender that publishes logback log entries to AWS CloudWatch
ISC License
56 stars 37 forks source link

MDC data is not included in log events #20

Closed fs-aikito closed 3 years ago

fs-aikito commented 6 years ago

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

j256 commented 6 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.

fs-aikito commented 6 years ago

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

j256 commented 3 years ago

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.

j256 commented 3 years ago

I think this has been fixed also by https://github.com/j256/cloudwatch-logback-appender/pull/31