KAFKA-18831 Migrating to log4j2 introduce behavior changes of adjusting level dynamically (#18969)

fix the following behavior changes.

1) in log4j 1, users can't change the logger by parent if the logger is declared by properties explicitly. For example, `org.apache.kafka.controller` has level explicitly in the properties. Hence, we can't use "org.apache.kafka=INFO" to change the level of `org.apache.kafka.controller` to INFO. By contrast, log4j2 allows us to change all child loggers by the parent logger.

2) in log4j2, we can change the level of root to impact all loggers' level. By contrast, log4j 1 can't. 

Reviewers: Chia-Ping Tsai <chia7712@gmail.com>
This commit is contained in:
TengYao Chi 2025-02-21 16:12:58 +08:00 committed by GitHub
parent acea35ddf3
commit d31cbf59de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 5 additions and 4 deletions

View File

@ -112,11 +112,11 @@ private class Log4jCoreController extends LoggingControllerDelegate {
val level = Level.toLevel(logLevel.toUpperCase(Locale.ROOT))
if (loggerName == ROOT_LOGGER) {
Configurator.setAllLevels(LogManager.ROOT_LOGGER_NAME, level)
Configurator.setLevel(LogManager.ROOT_LOGGER_NAME, level)
true
} else {
if (loggerExists(loggerName) && level != null) {
Configurator.setAllLevels(loggerName, level)
Configurator.setLevel(loggerName, level)
true
}
else false
@ -124,12 +124,13 @@ private class Log4jCoreController extends LoggingControllerDelegate {
}
override def unsetLogLevel(loggerName: String): Boolean = {
val nullLevel: Level = null
if (loggerName == ROOT_LOGGER) {
Configurator.setAllLevels(LogManager.ROOT_LOGGER_NAME, null)
Configurator.setLevel(LogManager.ROOT_LOGGER_NAME, nullLevel)
true
} else {
if (loggerExists(loggerName)) {
Configurator.setAllLevels(loggerName, null)
Configurator.setLevel(loggerName, nullLevel)
true
}
else false