Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

RetainingAppender does not persist lazily-initialized log context #98

@filipovskid

Description

@filipovskid

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() 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions