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.
This commit is contained in:
Joel Dice 2014-05-20 14:26:56 -06:00
parent b28142a837
commit 7a768f2c69
2 changed files with 45 additions and 2 deletions

View File

@ -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 {

View File

@ -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]);
}
}
}