changeset 1017:cea861983a46

Ensure logged messages are printed Reviewed-by: jerboaa, vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005822.html
author Omair Majid <omajid@redhat.com>
date Tue, 26 Feb 2013 15:01:33 -0500
parents efc284b7a127
children d33dc1c2d0ad
files agent/cli/src/main/java/com/redhat/thermostat/agent/cli/impl/AgentApplication.java common/core/src/main/java/com/redhat/thermostat/common/utils/LoggingUtils.java common/core/src/test/java/com/redhat/thermostat/common/utils/LoggingUtilsTest.java
diffstat 3 files changed, 29 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- 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);
 
--- 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
--- 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;
     }