This test is failing with Spring Boot 3.1.1. It works OK with Spring Boot 3.1.0.

@SpringBootTest
class LogbackBehaviorChangeApplicationTests {

    private final LoggerContext ctx = new LoggerContext();

    @Test
    void logback148BehaviorChangeTest() {
        LoggingEvent event = new LoggingEvent("fqcn", this.ctx.getLogger("logger"), Level.ALL,
                "logbackbehaviorchange", null, new Object[0]);

        MDC.put("property-1-clave1-en-MDC", "89777");
        MDC.put("property-2-clave2-en-MDC", "1.00009978273");
        MDC.put("property-3-clave3-en-MDC", "valor-3-en-mdc");
        final Map<String, String> mdcPropertyMap = event.getMDCPropertyMap();

        Assertions.assertNotNull(mdcPropertyMap);
        Assertions.assertEquals("89777", mdcPropertyMap.get("property-1-clave1-en-MDC"));
        Assertions.assertEquals("1.00009978273", mdcPropertyMap.get("property-2-clave2-en-MDC"));
        Assertions.assertEquals("valor-3-en-mdc", mdcPropertyMap.get("property-3-clave3-en-MDC"));

    }

}

If I keep using SB 3.1.1 but I downgrade Logback to 1.4.7, the test works.

So I think there is a bug in Logback 1.4.8 that provokes this malfunction. Maybe SB 3.1.2 should address the problem either by downgrading to Logback 1.4.7 or (if available) upgrading to a fixed Logback version.

You have the test project here: logback-behavior-change.zip

And the (partial) error log with Sb 3.1.1 is:

java.lang.NullPointerException: Cannot invoke "ch.qos.logback.classic.util.LogbackMDCAdapter.getCopyOfContextMap()" because "mdcAdapter" is null
    at ch.qos.logback.classic.spi.LoggingEvent.getMDCPropertyMap(LoggingEvent.java:411)
    at com.example.logbackbehaviorchange.LogbackBehaviorChangeApplicationTests.logback148BehaviorChangeTest(LogbackBehaviorChangeApplicationTests.java:26)

Comment From: ceki

@juliojgd Hello Julio,

As of logback 1.4..8 (commit ca7fbc7 LoggingEvent will fetch MDC data from the MDCAdapter attached to its LoggingContext. Previously, the MDCAdapter instance was located statically through the MDC.MDC.getMDCAdapter() method.

You can solve this problem by adding the following line to your test:

ctx.setMDCAdapter(MDC.getMDCAdapter()) where ctx is the LoggerContext defined in you test.

Comment From: juliojgd

Hello @ceki , thanks for your quick response

You can solve this problem by adding the following line to your test: ctx.setMDCAdapter(MDC.getMDCAdapter()) where ctx is the LoggerContext defined in you test.

Types do not match, I had to cast it this way: ctx.setMDCAdapter((LogbackMDCAdapter) MDC.getMDCAdapter()); (Personally I don't like that cast much)

Maybe this is an important change to check if Spring Boot+Logback is affected, as the Spring Boot PR for Logback 1.4.8 upgrade contains no other change that the version change itself... It sounds that something could go wrong with this behavior change, right?

Comment From: ceki

@juliojgd I don't think the issue described here is related to SpringBoot but to the assumptions made by the test provided above.

The test assumes that the mdcPropertyMap is static and can only be obtained through MDC.getMDCContextMap. In older versions of Logback, the LoggingEvent source code made the same assumption. This assumption no longer holds.

To answer your question, I expect the impact of the change to be limited to test cases.

Comment From: juliojgd

Understood, thanks @ceki

Comment From: wilkinsona

Thanks, @ceki.