HADOOP-13098. Dynamic LogLevel setting page should accept case-insensitive log level string. (Junping Du via xyao)
Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/4e5e87dd Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/4e5e87dd Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/4e5e87dd Branch: refs/heads/HDFS-1312 Commit: 4e5e87ddd4a47dbea2b23387782e7cd47dec560e Parents: f305d9c Author: Xiaoyu Yao <x...@apache.org> Authored: Thu May 5 17:56:31 2016 -0700 Committer: Xiaoyu Yao <x...@apache.org> Committed: Thu May 5 17:56:58 2016 -0700 ---------------------------------------------------------------------- .../java/org/apache/hadoop/log/LogLevel.java | 16 +++++--- .../org/apache/hadoop/log/TestLogLevel.java | 39 ++++++++++++++++++-- 2 files changed, 46 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hadoop/blob/4e5e87dd/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/log/LogLevel.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/log/LogLevel.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/log/LogLevel.java index baf71b6..8db0dce 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/log/LogLevel.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/log/LogLevel.java @@ -142,27 +142,33 @@ public class LogLevel { private static void process(org.apache.log4j.Logger log, String level, PrintWriter out) throws IOException { if (level != null) { - if (!level.equals(org.apache.log4j.Level.toLevel(level).toString())) { - out.println(MARKER + "Bad level : <b>" + level + "</b><br />"); + if (!level.equalsIgnoreCase(org.apache.log4j.Level.toLevel(level) + .toString())) { + out.println(MARKER + "Bad Level : <b>" + level + "</b><br />"); } else { log.setLevel(org.apache.log4j.Level.toLevel(level)); out.println(MARKER + "Setting Level to " + level + " ...<br />"); } } out.println(MARKER - + "Effective level: <b>" + log.getEffectiveLevel() + "</b><br />"); + + "Effective Level: <b>" + log.getEffectiveLevel() + "</b><br />"); } private static void process(java.util.logging.Logger log, String level, PrintWriter out) throws IOException { if (level != null) { - log.setLevel(java.util.logging.Level.parse(level)); + String levelToUpperCase = level.toUpperCase(); + try { + log.setLevel(java.util.logging.Level.parse(levelToUpperCase)); + } catch (IllegalArgumentException e) { + out.println(MARKER + "Bad Level : <b>" + level + "</b><br />"); + } out.println(MARKER + "Setting Level to " + level + " ...<br />"); } java.util.logging.Level lev; for(; (lev = log.getLevel()) == null; log = log.getParent()); - out.println(MARKER + "Effective level: <b>" + lev + "</b><br />"); + out.println(MARKER + "Effective Level: <b>" + lev + "</b><br />"); } } } http://git-wip-us.apache.org/repos/asf/hadoop/blob/4e5e87dd/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/log/TestLogLevel.java ---------------------------------------------------------------------- diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/log/TestLogLevel.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/log/TestLogLevel.java index 0f0a7c3..e35323b 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/log/TestLogLevel.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/log/TestLogLevel.java @@ -28,6 +28,7 @@ import junit.framework.TestCase; import org.apache.commons.logging.*; import org.apache.commons.logging.impl.*; import org.apache.log4j.*; +import org.junit.Assert; public class TestLogLevel extends TestCase { static final PrintStream out = System.out; @@ -42,12 +43,13 @@ public class TestLogLevel extends TestCase { log.debug("log.debug1"); log.info("log.info1"); log.error("log.error1"); - assertTrue(!Level.ERROR.equals(log.getEffectiveLevel())); + Assert.assertNotEquals("Get default Log Level which shouldn't be ERROR.", + Level.ERROR, log.getEffectiveLevel()); HttpServer2 server = new HttpServer2.Builder().setName("..") .addEndpoint(new URI("http://localhost:0")).setFindPort(true) .build(); - + server.start(); String authority = NetUtils.getHostPortString(server .getConnectorAddress(0)); @@ -67,7 +69,8 @@ public class TestLogLevel extends TestCase { log.debug("log.debug2"); log.info("log.info2"); log.error("log.error2"); - assertTrue(Level.ERROR.equals(log.getEffectiveLevel())); + assertEquals("Try setting log level: ERROR from servlet.", Level.ERROR, + log.getEffectiveLevel()); //command line String[] args = {"-setlevel", authority, logName, Level.DEBUG.toString()}; @@ -75,7 +78,35 @@ public class TestLogLevel extends TestCase { log.debug("log.debug3"); log.info("log.info3"); log.error("log.error3"); - assertTrue(Level.DEBUG.equals(log.getEffectiveLevel())); + assertEquals("Try setting log level: DEBUG via command line", Level.DEBUG, + log.getEffectiveLevel()); + + // Test mixed upper case and lower case in level string. + String[] args2 = {"-setlevel", authority, logName, "Info"}; + LogLevel.main(args2); + log.debug("log.debug4"); + log.info("log.info4"); + log.error("log.error4"); + assertEquals("Try setting log level: Info via command line.", Level.INFO, + log.getEffectiveLevel()); + + // Test "Error" instead of "ERROR" should work for servlet + URL newUrl = new URL("http://" + authority + "/logLevel?log=" + logName + + "&level=" + "Error"); + out.println("*** Connecting to " + newUrl); + connection = newUrl.openConnection(); + connection.connect(); + + BufferedReader in2 = new BufferedReader(new InputStreamReader( + connection.getInputStream())); + for(String line; (line = in2.readLine()) != null; out.println(line)); + in2.close(); + + log.debug("log.debug5"); + log.info("log.info5"); + log.error("log.error5"); + assertEquals("Try setting log level: Error via servlet.", Level.ERROR, + log.getEffectiveLevel()); } else { out.println(testlog.getClass() + " not tested."); --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org For additional commands, e-mail: common-commits-h...@hadoop.apache.org