-
Notifications
You must be signed in to change notification settings - Fork 14.5k
KAFKA-18834: Fix LoggingResourceTest#testSetLevelDefaultScope #19920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KAFKA-18834: Fix LoggingResourceTest#testSetLevelDefaultScope #19920
Conversation
Verified that this change resolves the issue by running the following command: ./gradlew cleanTest \
connect:runtime:test \
--tests LoggersTest.testSetLevelWithValidRootLoggerNames \
--tests LoggingResourceTest.testSetLevelDefaultScope \
-PmaxParallelForks=1 Both tests passed successfully: Test Resultskafka on KAFKA-18834-Fix-LoggingResourceTest#testSetLevelDefaultScope- [$!] …
➜ ./gradlew cleanTest \
connect:runtime:test \
--tests LoggersTest.testSetLevelWithValidRootLoggerNames \
--tests LoggingResourceTest.testSetLevelDefaultScope \
-PmaxParallelForks=1 \
> Configure project :
Starting build with version 4.1.0-SNAPSHOT (commit id 82ea9d0f) using Gradle 8.14.1, Java 21 and Scala 2.13.16
Build properties: ignoreFailures=false, maxParallelForks=1, maxScalacThreads=8, maxTestRetries=0
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
> Task :connect:runtime:test
Gradle Test Run :connect:runtime:test > Gradle Test Executor 19 > LoggersTest > testSetLevelWithValidRootLoggerNames() PASSED
Gradle Test Run :connect:runtime:test > Gradle Test Executor 19 > LoggingResourceTest > testSetLevelDefaultScope() PASSED
BUILD SUCCESSFUL in 2s
153 actionable tasks: 2 executed, 151 up-to-date |
|
||
@AfterEach | ||
public void teardown() { | ||
Configurator.setAllLevels(LogManager.ROOT_LOGGER_NAME, originalRootLevel); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes all 'child' loggers, right? If so, it is not a kind of restoring logger levels
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chia7712
Thank you for catching this!
After rethinking the solution, I confirmed that calling setLevel
in LoggersTest will overwrite every child loggers (see [1]). That means any test in LoggersTest
that invokes setLevel
risks leaking its changes into other test classes.
Instead of recording and restoring each logger's previous level, reloading the Log4j configuration after each test may be a simpler solution. This ensures that any global Log4j configuration changes made in LoggersTest
are fully reset, and that the restored configuration matches what other tests would see when run independently, which maintains proper test isolation.
I’ve updated the teardown to call LoggerContext.getContext(false).reconfigure()
. Please let me know if you have any further feedback!
…ggingResourceTest#testSetLevelDefaultScope-
…ggingResourceTest#testSetLevelDefaultScope-
I ran a few more test classes that use logging with the command below. They all passed successfully when executed in the same JVM.
|
|
||
@AfterEach | ||
public void tearDown() { | ||
LoggerContext.getContext(false).reconfigure(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add comments for this reconfigure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late update, I've added the comments.
…ggingResourceTest#testSetLevelDefaultScope-
Jira: KAFKA-18834
Flaky behavior
LoggingResourceTest#testSetLevelDefaultScope
sometimes fails by notcapturing its expected WARN log.
Root cause
Both
LoggersTest#testSetLevelWithValidRootLoggerNames
andLoggingResourceTest#testSetLevelDefaultScope
may share the sameLoggerContext
when executed in the same JVM.LoggersTest#testSetLevelWithValidRootLoggerNames
callsloggers.setLevel("", ERROR)
, which mutates the global root loggerlevel to ERROR and suppresses WARN logs, which causes subsequent tests
to fail to emit WARN-level output.
Fix in this PR
Resets the Log4j configuration after each test in
LoggersTest
,ensuring that any global changes are reverted.
Reviewers: Chia-Ping Tsai chia7712@gmail.com