diff --git a/core/src/main/scala/org/apache/spark/util/Utils.scala b/core/src/main/scala/org/apache/spark/util/Utils.scala index 996af86bf5b2c..4c7160cc9b82b 100644 --- a/core/src/main/scala/org/apache/spark/util/Utils.scala +++ b/core/src/main/scala/org/apache/spark/util/Utils.scala @@ -57,6 +57,8 @@ import org.apache.hadoop.io.compress.{CompressionCodecFactory, SplittableCompres import org.apache.hadoop.security.UserGroupInformation import org.apache.hadoop.util.{RunJar, StringUtils} import org.apache.hadoop.yarn.conf.YarnConfiguration +import org.apache.logging.log4j.{Level, LogManager} +import org.apache.logging.log4j.core.LoggerContext import org.eclipse.jetty.util.MultiException import org.slf4j.Logger @@ -2423,11 +2425,13 @@ private[spark] object Utils extends Logging { /** * configure a new log4j level */ - def setLogLevel(l: org.apache.logging.log4j.Level): Unit = { - val rootLogger = org.apache.logging.log4j.LogManager.getRootLogger() - .asInstanceOf[org.apache.logging.log4j.core.Logger] - rootLogger.setLevel(l) - rootLogger.get().setLevel(l) + def setLogLevel(l: Level): Unit = { + val ctx = LogManager.getContext(false).asInstanceOf[LoggerContext] + val config = ctx.getConfiguration() + val loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME) + loggerConfig.setLevel(l) + ctx.updateLoggers() + // Setting threshold to null as rootLevel will define log level for spark-shell Logging.sparkShellThresholdLevel = null } diff --git a/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala b/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala index 3c5f50082adb8..bfb1bc3f029c9 100644 --- a/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala +++ b/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala @@ -690,8 +690,11 @@ class UtilsSuite extends SparkFunSuite with ResetSystemProperties with Logging { try { Utils.setLogLevel(org.apache.logging.log4j.Level.ALL) assert(rootLogger.getLevel == org.apache.logging.log4j.Level.ALL) + assert(log.isInfoEnabled()) Utils.setLogLevel(org.apache.logging.log4j.Level.ERROR) assert(rootLogger.getLevel == org.apache.logging.log4j.Level.ERROR) + assert(!log.isInfoEnabled()) + assert(log.isErrorEnabled()) } finally { // Best effort at undoing changes this test made. Utils.setLogLevel(current)