Skip to content

Commit c1967f5

Browse files
authored
test(spring-boot): Keep the Log4j2 LoggerContext reachable in appender auto-configuration tests (#6488)
1 parent 63a675a commit c1967f5

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎sentry-spring-boot-4/src/test/kotlin/io/sentry/spring/boot4/SentryLog4j2AppenderAutoConfigurationTest.kt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ class SentryLog4j2AppenderAutoConfigurationTest {
7272

7373
private val originalLogManagerFactory = LogManager.getFactory()
7474

75-
private val loggerContext: LoggerContext
76-
get() = LogManager.getContext(false) as LoggerContext
75+
// Log4j2 holds LoggerContexts weakly, so a GC during context startup would replace this one
76+
// (and drop appenders added by a test) unless the test keeps it reachable.
77+
private lateinit var loggerContext: LoggerContext
7778

7879
private val configuration
7980
get() = loggerContext.configuration
@@ -85,6 +86,7 @@ class SentryLog4j2AppenderAutoConfigurationTest {
8586
fun `reset Log4j2 context`() {
8687
useLog4j2Core()
8788
resetLog4j2Context()
89+
loggerContext = LogManager.getContext(false) as LoggerContext
8890
}
8991

9092
@AfterTest

‎sentry-spring-boot-jakarta/src/test/kotlin/io/sentry/spring/boot/jakarta/SentryLog4j2AppenderAutoConfigurationTest.kt‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ class SentryLog4j2AppenderAutoConfigurationTest {
7272

7373
private val originalLogManagerFactory = LogManager.getFactory()
7474

75-
private val loggerContext: LoggerContext
76-
get() = LogManager.getContext(false) as LoggerContext
75+
// Log4j2 holds LoggerContexts weakly, so a GC during context startup would replace this one
76+
// (and drop appenders added by a test) unless the test keeps it reachable.
77+
private lateinit var loggerContext: LoggerContext
7778

7879
private val configuration
7980
get() = loggerContext.configuration
@@ -85,6 +86,7 @@ class SentryLog4j2AppenderAutoConfigurationTest {
8586
fun `reset Log4j2 context`() {
8687
useLog4j2Core()
8788
resetLog4j2Context()
89+
loggerContext = LogManager.getContext(false) as LoggerContext
8890
}
8991

9092
@AfterTest

0 commit comments

Comments
 (0)