# HG changeset patch # User Omair Majid # Date 1361908893 18000 # Node ID cea861983a46ef6d46fc905d20fe74d74d071a7c # Parent efc284b7a127f63374b3dd2b80c10adf67ea888a Ensure logged messages are printed Reviewed-by: jerboaa, vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005822.html diff -r efc284b7a127 -r cea861983a46 agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/AgentApplication.java --- a/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/AgentApplication.java Thu Feb 28 02:23:07 2013 -0500 +++ b/agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/AgentApplication.java Tue Feb 26 15:01:33 2013 -0500 @@ -109,7 +109,7 @@ configuration.setStartTime(startTime); if (configuration.isDebugConsole()) { - LoggingUtils.useDevelConsole(); + LoggingUtils.enableConsoleLogging(); } final Logger logger = LoggingUtils.getLogger(AgentApplication.class); diff -r efc284b7a127 -r cea861983a46 common/core/src/main/java/com/redhat/thermostat/common/utils/LoggingUtils.java --- a/common/core/src/main/java/com/redhat/thermostat/common/utils/LoggingUtils.java Thu Feb 28 02:23:07 2013 -0500 +++ b/common/core/src/main/java/com/redhat/thermostat/common/utils/LoggingUtils.java Tue Feb 26 15:01:33 2013 -0500 @@ -59,31 +59,36 @@ public final class LoggingUtils { // package private for testing - static Logger root = null; - // package private for testing static final String ROOTNAME = "com.redhat.thermostat"; + + private static final Logger root; + + private static final ConsoleHandler handler; + private static final String HANDLER_PROP = ROOTNAME + ".handlers"; private static final String LOG_LEVEL_PROP = ROOTNAME + ".level"; private static final String DEFAULT_LOG_HANDLER = "java.util.logging.ConsoleHandler"; private static final Level DEFAULT_LOG_LEVEL = Level.INFO; - private LoggingUtils() { - /* should not be instantiated */ + static { + root = Logger.getLogger(ROOTNAME); + root.setUseParentHandlers(false); + for (Handler handler : root.getHandlers()) { + handler.setFormatter(new LogFormatter()); + // This is workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4462908 + handler.setLevel(Level.ALL); + } + + handler = new ConsoleHandler(); + handler.setFormatter(new LogFormatter()); + // This is workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4462908 + handler.setLevel(Level.ALL); + + enableConsoleLogging(); } - /** - * Pretty much every one of the utility methods in this static class should call this method before doing anything else. - */ - private static void ensureRootLogger() { - if (root == null) { - root = Logger.getLogger(ROOTNAME); - root.setUseParentHandlers(false); - for (Handler handler : root.getHandlers()) { - handler.setFormatter(new LogFormatter()); - // This is workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4462908 - handler.setLevel(Level.ALL); - } - } + private LoggingUtils() { + /* should not be instantiated */ } /** @@ -92,7 +97,6 @@ * @param level the minimum level at which logging statements should appear in the logs */ public static void setGlobalLogLevel(Level level) { - ensureRootLogger(); root.setLevel(level); } @@ -100,7 +104,6 @@ * Returns an appropriate logger to be used by class klass. */ public static Logger getLogger(Class klass) { - ensureRootLogger(); Logger logger = Logger.getLogger(klass.getPackage().getName()); logger.setLevel(null); // Will inherit from root logger return logger; @@ -109,15 +112,15 @@ /** * Ensures log messages are written to the console as well */ - public static void useDevelConsole() { - ensureRootLogger(); - ConsoleHandler handler = new ConsoleHandler(); - handler.setFormatter(new LogFormatter()); - // This is workaround for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4462908 - handler.setLevel(Level.ALL); + public static void enableConsoleLogging() { + root.removeHandler(handler); root.addHandler(handler); } + public static void disableConsoleLogging() { + root.removeHandler(handler); + } + public static void loadGlobalLoggingConfig() throws InvalidConfigurationException { File thermostatConfigurationDir = new File(new Configuration().getConfigurationDir()); File loggingPropertiesFile = new File(thermostatConfigurationDir, "logging.properties"); @@ -140,8 +143,6 @@ private static void readLoggingProperties(File loggingPropertiesFile) throws InvalidConfigurationException { - // Make sure root logger exists - ensureRootLogger(); try (FileInputStream fis = new FileInputStream(loggingPropertiesFile)){ // Set basic logger configs. Note that this does NOT add handlers. // It also resets() handlers. I.e. removes any existing handlers diff -r efc284b7a127 -r cea861983a46 common/core/src/test/java/com/redhat/thermostat/common/utils/LoggingUtilsTest.java --- a/common/core/src/test/java/com/redhat/thermostat/common/utils/LoggingUtilsTest.java Thu Feb 28 02:23:07 2013 -0500 +++ b/common/core/src/test/java/com/redhat/thermostat/common/utils/LoggingUtilsTest.java Tue Feb 26 15:01:33 2013 -0500 @@ -71,7 +71,6 @@ @After public void tearDown() { - LoggingUtils.root = null; mongodbLogger = null; }