From bf8c856a3c911649debaa41450dafeb1e6cc5f07 Mon Sep 17 00:00:00 2001 From: Eric Scharff Date: Thu, 19 Feb 2009 15:53:10 -0700 Subject: [PATCH] Partially handle cascading logging levels. We now maintain a virtual root loger, on which you can set the log level. When any logger logs, it finds the effective log level by going up the parent chain, and finding a meaningful log lvel. Thus, one can now do Logger.getLogger("").setLevel(Level.FINER) and set the log level for all other loggers (that do not specify their own default) to the level specified. --- classpath/java/util/logging/Logger.java | 42 ++++++++++++++++++++----- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/classpath/java/util/logging/Logger.java b/classpath/java/util/logging/Logger.java index 5a1e7ee055..6fb968dd83 100644 --- a/classpath/java/util/logging/Logger.java +++ b/classpath/java/util/logging/Logger.java @@ -16,15 +16,23 @@ import java.util.List; public class Logger { private final String name; - private int levelValue = Level.INFO.intValue(); + private Level levelValue = null; private static final ArrayList handlers; + private static Logger rootLogger; + private Logger parent; + static { + rootLogger = new Logger(""); + rootLogger.setLevel(Level.INFO); handlers = new ArrayList(); handlers.add(new DefaultHandler()); } public static Logger getLogger(String name) { - return new Logger(name); + if (name.equals("")) return rootLogger; + Logger logger = new Logger(name); + logger.parent = rootLogger; + return logger; } private Logger(String name) { @@ -43,6 +51,10 @@ public class Logger { handlers.remove(handler); } + public Logger getParent() { + return parent; + } + public void fine(String message) { log(Level.FINE, Method.getCaller(), message, null); } @@ -81,12 +93,26 @@ public class Logger { } publish(new LogRecord(name, sourceMethod, level, msg, thrown)); } - + + public Level getLevel() { + return levelValue; + } + + private Level getEffectiveLevel() { + Logger logger = this; + + while (logger.levelValue == null) { + logger = logger.getParent(); + } + return logger.getLevel(); + } + private void log(Level level, Method caller, String message, Throwable exception) { - if (level.intValue()= levelValue; + return level.intValue() >= levelValue.intValue(); } private static class DefaultHandler extends Handler {