Skip to content

Commit 1431a4a

Browse files
viiryadongjoon-hyun
authored andcommitted
[SPARK-37887][CORE] Fix the check of repl log level
### What changes were proposed in this pull request? This patch fixes the check of repl's log level. So we can correctly know if the repl class is set with log level or not. ### Why are the changes needed? Same as the check in `SparkShellLoggingFilter`, `getLevel` cannot be used anymore to check if the log level is set or not for a logger in log4j2. ### Does this PR introduce _any_ user-facing change? No ### How was this patch tested? Manual verified locally. Closes #35198 from viirya/SPARK-37887. Authored-by: Liang-Chi Hsieh <viirya@gmail.com> Signed-off-by: Dongjoon Hyun <dongjoon@apache.org>
1 parent f7dd37c commit 1431a4a

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

core/src/main/scala/org/apache/spark/internal/Logging.scala

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ trait Logging {
154154
// Use the repl's main class to define the default log level when running the shell,
155155
// overriding the root logger's config if they're different.
156156
val replLogger = LogManager.getLogger(logName).asInstanceOf[Log4jLogger]
157-
val replLevel = Option(replLogger.getLevel()).getOrElse(Level.WARN)
157+
val replLevel = if (Logging.loggerWithCustomConfig(replLogger)) {
158+
replLogger.getLevel()
159+
} else {
160+
Level.WARN
161+
}
158162
// Update the consoleAppender threshold to replLevel
159163
if (replLevel != rootLogger.getLevel()) {
160164
if (!silent) {
@@ -229,6 +233,17 @@ private[spark] object Logging {
229233
"org.apache.logging.slf4j.Log4jLoggerFactory".equals(binderClass)
230234
}
231235

236+
// Return true if the logger has custom configuration. It depends on:
237+
// 1. If the logger isn't attached with root logger config (i.e., with custom configuration), or
238+
// 2. the logger level is different to root config level (i.e., it is changed programmatically).
239+
//
240+
// Note that if a logger is programmatically changed log level but set to same level
241+
// as root config level, we cannot tell if it is with custom configuration.
242+
private def loggerWithCustomConfig(logger: Log4jLogger): Boolean = {
243+
val rootConfig = LogManager.getRootLogger.asInstanceOf[Log4jLogger].get()
244+
(logger.get() ne rootConfig) || (logger.getLevel != rootConfig.getLevel())
245+
}
246+
232247
/**
233248
* Return true if log4j2 is initialized by default configuration which has one
234249
* appender with error level. See `org.apache.logging.log4j.core.config.DefaultConfiguration`.
@@ -267,17 +282,6 @@ private[spark] object Logging {
267282
}
268283
}
269284

270-
// Return true if the logger has custom configuration. It depends on:
271-
// 1. If the logger isn't attached with root logger config (i.e., with custom configuration), or
272-
// 2. the logger level is different to root config level (i.e., it is changed programmatically).
273-
//
274-
// Note that if a logger is programmatically changed log level but set to same level
275-
// as root config level, we cannot tell if it is with custom configuration.
276-
private def loggerWithCustomConfig(logger: Log4jLogger): Boolean = {
277-
val rootConfig = LogManager.getRootLogger.asInstanceOf[Log4jLogger].get()
278-
(logger.get() ne rootConfig) || (logger.getLevel != rootConfig.getLevel())
279-
}
280-
281285
override def getState: LifeCycle.State = status
282286

283287
override def initialize(): Unit = {

0 commit comments

Comments
 (0)