netceteragroup / girders

Java Framework based on spring-boot
https://netceteragroup.github.io/girders/
Apache License 2.0
5 stars 1 forks source link

RetainingAppender does not persist lazily-initialized log context #98

Open filipovskid opened 2 weeks ago

filipovskid commented 2 weeks ago

Description

When buffering ILoggingEvent log events, the RetainingAppender does not persist the event's lazily-initialized fields, such as the MDC (Mapped Diagnostic Context) and thread name. As a result, when the retained logs are eventually dumped, these fields are initialized to the current context rather than the context at the time the log event was created. This behavior leads to incorrect log information, which can cause confusion and misinterpretation of log data.

Steps to Reproduce:

  1. Configure Logback with a RetainingAppender that buffers log events and pipes/dumps them to an Appender that does not invoke ILoggingEvent#prepareForDeferredProcessing. 1.1. Alternative: Since it is easiest to test with a ConsoleAppender, which does prepare the event for deferred processing, another way to reproduce the issue is to only log events whose level is below the threshold and finally trigger an event that dumps the buffer.
  2. Log multiple entries while updating the MDC between each log.
  3. Trigger an event that causes the RetainingAppender to flush the buffered logs. Result: The flushed log events will incorrectly reflect the current MDC context rather than the context at the time each event was logged.

I will provide a test case in the upcoming PR that reproduces this issue.

Solution

Invoke [ILoggingEvent#prepareForDeferredProcessing()](https://logback.qos.ch/apidocs/ch.qos.logback.classic/ch/qos/logback/classic/spi/LoggingEvent.html#prepareForDeferredProcessing()) before buffering the log event. According to the documentation, this method should be called to ensure context data like MDC and thread name are properly captured for asynchronous or deferred logging.

filipovskid commented 2 weeks ago

I will open a PR shortly.