dongjinleekr commented on a change in pull request #7898: URL: https://github.com/apache/kafka/pull/7898#discussion_r501048158
########## File path: core/src/main/scala/kafka/utils/Log4jController.scala ########## @@ -20,62 +20,73 @@ package kafka.utils import java.util import java.util.Locale -import org.apache.log4j.{Level, LogManager, Logger} +import org.apache.logging.log4j.core.LoggerContext +import org.apache.logging.log4j.{Level, LogManager} +import org.apache.logging.log4j.core.config.{Configurator, LoggerConfig} -import scala.collection.mutable import scala.jdk.CollectionConverters._ object Log4jController { + + /** + * Note: In log4j, the root logger's name was "root" and Kafka also followed that name for dynamic logging control feature. + * + * The root logger's name is changed in log4j2 to empty string (see: [[LogManager.ROOT_LOGGER_NAME]]) but for backward- + * compatibility. Kafka keeps its original root logger name. It is why here is a dedicated definition for the root logger name. + */ val ROOT_LOGGER = "root" /** * Returns a map of the log4j loggers and their assigned log level. * If a logger does not have a log level assigned, we return the root logger's log level */ - def loggers: mutable.Map[String, String] = { - val logs = new mutable.HashMap[String, String]() - val rootLoggerLvl = existingLogger(ROOT_LOGGER).getLevel.toString - logs.put(ROOT_LOGGER, rootLoggerLvl) - - val loggers = LogManager.getCurrentLoggers - while (loggers.hasMoreElements) { - val logger = loggers.nextElement().asInstanceOf[Logger] - if (logger != null) { - val level = if (logger.getLevel != null) logger.getLevel.toString else rootLoggerLvl - logs.put(logger.getName, level) - } - } - logs + def loggers: Map[String, String] = { + val logContext = LogManager.getContext(false).asInstanceOf[LoggerContext] + val rootLoggerLevel = logContext.getRootLogger.getLevel.toString + + logContext.getLoggers.asScala + .filter(_.getName != LogManager.ROOT_LOGGER_NAME) + .map { logger => + val effectiveLevel = if (logger.getLevel != null) logger.getLevel.toString else rootLoggerLevel Review comment: > But I guess you're waiting for my PR to be merged, right? Exactly. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org