From c595d6f9fa0f0d74aab4719d4133d353a17d3a06 Mon Sep 17 00:00:00 2001 From: Markus Hintersteiner Date: Fri, 6 Nov 2026 09:58:13 +0100 Subject: [PATCH] test(spring-boot): Keep the Log4j2 LoggerContext reachable in appender auto-configuration tests --- .../boot4/SentryLog4j2AppenderAutoConfigurationTest.kt | 6 ++++-- .../jakarta/SentryLog4j2AppenderAutoConfigurationTest.kt | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryLog4j2AppenderAutoConfigurationTest.kt b/sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryLog4j2AppenderAutoConfigurationTest.kt index b9bcee11d38..bc6d8c88ca3 100644 --- a/sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryLog4j2AppenderAutoConfigurationTest.kt +++ b/sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryLog4j2AppenderAutoConfigurationTest.kt @@ -72,8 +72,9 @@ class SentryLog4j2AppenderAutoConfigurationTest { private val originalLogManagerFactory = LogManager.getFactory() - private val loggerContext: LoggerContext - get() = LogManager.getContext(false) as LoggerContext + // Log4j2 holds LoggerContexts weakly, so a GC during context startup would replace this one + // (and drop appenders added by a test) unless the test keeps it reachable. + private lateinit var loggerContext: LoggerContext private val configuration get() = loggerContext.configuration @@ -85,6 +86,7 @@ class SentryLog4j2AppenderAutoConfigurationTest { fun `reset Log4j2 context`() { useLog4j2Core() resetLog4j2Context() + loggerContext = LogManager.getContext(false) as LoggerContext } @AfterTest diff --git a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryLog4j2AppenderAutoConfigurationTest.kt b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryLog4j2AppenderAutoConfigurationTest.kt index 08eb6ac45c7..7cdb97cfc9a 100644 --- a/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryLog4j2AppenderAutoConfigurationTest.kt +++ b/sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryLog4j2AppenderAutoConfigurationTest.kt @@ -72,8 +72,9 @@ class SentryLog4j2AppenderAutoConfigurationTest { private val originalLogManagerFactory = LogManager.getFactory() - private val loggerContext: LoggerContext - get() = LogManager.getContext(false) as LoggerContext + // Log4j2 holds LoggerContexts weakly, so a GC during context startup would replace this one + // (and drop appenders added by a test) unless the test keeps it reachable. + private lateinit var loggerContext: LoggerContext private val configuration get() = loggerContext.configuration @@ -85,6 +86,7 @@ class SentryLog4j2AppenderAutoConfigurationTest { fun `reset Log4j2 context`() { useLog4j2Core() resetLog4j2Context() + loggerContext = LogManager.getContext(false) as LoggerContext } @AfterTest