From 44d6577c5031a5b8ce7cf66d5849b5269ad603f2 Mon Sep 17 00:00:00 2001 From: Holden Karau Date: Wed, 17 Sep 2014 11:26:44 -0700 Subject: [PATCH] add a util method for changing the log level while running --- .../src/main/scala/org/apache/spark/util/Utils.scala | 8 ++++++++ .../scala/org/apache/spark/util/UtilsSuite.scala | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) 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 c76b7af18481d..461aa290f4f53 100644 --- a/core/src/main/scala/org/apache/spark/util/Utils.scala +++ b/core/src/main/scala/org/apache/spark/util/Utils.scala @@ -1452,6 +1452,14 @@ private[spark] object Utils extends Logging { } } + /** + * configure a new log4j level + */ + def setLogLevel(l: org.apache.log4j.Level) { + org.apache.log4j.Logger.getRootLogger().setLevel(l) + } + + /** * config a log4j properties used for testsuite */ 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 70d423ba8a04d..c1639d6d2040b 100644 --- a/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala +++ b/core/src/test/scala/org/apache/spark/util/UtilsSuite.scala @@ -27,7 +27,9 @@ import com.google.common.base.Charsets import com.google.common.io.Files import org.scalatest.FunSuite -class UtilsSuite extends FunSuite { +import org.apache.spark.Logging + +class UtilsSuite extends FunSuite with Logging { test("bytesToString") { assert(Utils.bytesToString(10) === "10.0 B") @@ -297,4 +299,12 @@ class UtilsSuite extends FunSuite { } } + // Test for using the util function to change our log levels. + test("log4j log level change") { + Utils.setLogLevel(org.apache.log4j.Level.ALL) + assert(log.isInfoEnabled()) + Utils.setLogLevel(org.apache.log4j.Level.ERROR) + assert(!log.isInfoEnabled()) + assert(log.isErrorEnabled()) + } }