Skip to content

Commit

Permalink
[SPARK-15681][CORE] allow lowercase or mixed case log level string wh…
Browse files Browse the repository at this point in the history
…en calling sc.setLogLevel

## What changes were proposed in this pull request?
Currently `SparkContext API setLogLevel(level: String) `can not handle lower case or mixed case input string. But `org.apache.log4j.Level.toLevel` can take lowercase or mixed case.

This PR is to allow case-insensitive user input for the log level.

## How was this patch tested?
A unit testcase is added.

Author: Xin Wu <[email protected]>

Closes #13422 from xwu0226/reset_loglevel.
  • Loading branch information
xwu0226 authored and Marcelo Vanzin committed Jun 3, 2016
1 parent 61b80d5 commit 28ad0f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
16 changes: 9 additions & 7 deletions core/src/main/scala/org/apache/spark/SparkContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.spark
import java.io._
import java.lang.reflect.Constructor
import java.net.URI
import java.util.{Arrays, Properties, ServiceLoader, UUID}
import java.util.{Arrays, Locale, Properties, ServiceLoader, UUID}
import java.util.concurrent.ConcurrentMap
import java.util.concurrent.atomic.{AtomicBoolean, AtomicInteger, AtomicReference}

Expand Down Expand Up @@ -356,12 +356,12 @@ class SparkContext(config: SparkConf) extends Logging with ExecutorAllocationCli
* Valid log levels include: ALL, DEBUG, ERROR, FATAL, INFO, OFF, TRACE, WARN
*/
def setLogLevel(logLevel: String) {
val validLevels = Seq("ALL", "DEBUG", "ERROR", "FATAL", "INFO", "OFF", "TRACE", "WARN")
if (!validLevels.contains(logLevel)) {
throw new IllegalArgumentException(
s"Supplied level $logLevel did not match one of: ${validLevels.mkString(",")}")
}
Utils.setLogLevel(org.apache.log4j.Level.toLevel(logLevel))
// let's allow lowcase or mixed case too
val upperCased = logLevel.toUpperCase(Locale.ENGLISH)
require(SparkContext.VALID_LOG_LEVELS.contains(upperCased),
s"Supplied level $logLevel did not match one of:" +
s" ${SparkContext.VALID_LOG_LEVELS.mkString(",")}")
Utils.setLogLevel(org.apache.log4j.Level.toLevel(upperCased))
}

try {
Expand Down Expand Up @@ -2179,6 +2179,8 @@ class SparkContext(config: SparkConf) extends Logging with ExecutorAllocationCli
* various Spark features.
*/
object SparkContext extends Logging {
private val VALID_LOG_LEVELS =
Set("ALL", "DEBUG", "ERROR", "FATAL", "INFO", "OFF", "TRACE", "WARN")

/**
* Lock that guards access to global variables that track SparkContext construction.
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/scala/org/apache/spark/SparkContextSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -363,4 +363,19 @@ class SparkContextSuite extends SparkFunSuite with LocalSparkContext {
sc.stop()
assert(result == null)
}

test("log level case-insensitive and reset log level") {
sc = new SparkContext(new SparkConf().setAppName("test").setMaster("local"))
val originalLevel = org.apache.log4j.Logger.getRootLogger().getLevel
try {
sc.setLogLevel("debug")
assert(org.apache.log4j.Logger.getRootLogger().getLevel === org.apache.log4j.Level.DEBUG)
sc.setLogLevel("INfo")
assert(org.apache.log4j.Logger.getRootLogger().getLevel === org.apache.log4j.Level.INFO)
} finally {
sc.setLogLevel(originalLevel.toString)
assert(org.apache.log4j.Logger.getRootLogger().getLevel === originalLevel)
sc.stop()
}
}
}

0 comments on commit 28ad0f7

Please sign in to comment.