From 7a768f2c6955ec3b6ce91ee0bcd6554f061429e9 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Tue, 20 May 2014 14:26:56 -0600 Subject: [PATCH] fix log level inheritance A Logger which has not had a level set explicitly should inherit its effective level from its parent, not just default to INFO. --- classpath/java/util/logging/Logger.java | 11 ++++++-- test/Logging.java | 36 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/classpath/java/util/logging/Logger.java b/classpath/java/util/logging/Logger.java index 7623c26b83..5a86aabcde 100644 --- a/classpath/java/util/logging/Logger.java +++ b/classpath/java/util/logging/Logger.java @@ -32,7 +32,6 @@ public class Logger { if (name.equals("")) return rootLogger; Logger logger = new Logger(name); logger.parent = rootLogger; - logger.setLevel(Level.INFO); return logger; } @@ -60,6 +59,14 @@ public class Logger { log(Level.FINE, Method.getCaller(), message, null); } + public void finer(String message) { + log(Level.FINER, Method.getCaller(), message, null); + } + + public void finest(String message) { + log(Level.FINEST, Method.getCaller(), message, null); + } + public void info(String message) { log(Level.INFO, Method.getCaller(), message, null); } @@ -155,7 +162,7 @@ public class Logger { } public boolean isLoggable(Level level) { - return level.intValue() >= levelValue.intValue(); + return level.intValue() >= getEffectiveLevel().intValue(); } private static class DefaultHandler extends Handler { diff --git a/test/Logging.java b/test/Logging.java index c7e5b4d6ae..1f696574c6 100644 --- a/test/Logging.java +++ b/test/Logging.java @@ -95,6 +95,10 @@ public class Logging { private void e() throws Exception { throw new Exception("Started here"); } + + private static void expect(boolean v) { + if (! v) throw new RuntimeException(); + } private static final boolean useCustomHandler = true; public static void main(String args[]) { @@ -107,5 +111,37 @@ public class Logging { Logging me = new Logging(); me.run(); + + { final boolean[] logged = new boolean[1]; + Logger root = Logger.getLogger(""); + for (Handler h : root.getHandlers()) root.removeHandler(h); + root.setLevel(Level.FINER); + root.addHandler(new Handler() { + public void publish(LogRecord r) { + logged[0] = true; + } + }); + + Logger foo = Logger.getLogger("foo"); + expect(foo.getLevel() == null); + expect(foo.getParent() == root); + + foo.info("hi"); + expect(logged[0]); + + logged[0] = false; + foo.fine("hi"); + expect(logged[0]); + + logged[0] = false; + foo.finest("hi"); + expect(! logged[0]); + + root.setLevel(Level.FINEST); + + logged[0] = false; + foo.finest("hi"); + expect(logged[0]); + } } }