[logback-dev] svn commit: r1757 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic main/java/ch/qos/logback/classic/jmx main/java/ch/qos/logback/classic/net main/java/ch/qos/logback/classic/selector main/java/ch/qos/logback/classic/util main/java/org/slf4j/impl test/java/ch/qos/logback/classic/util

noreply.ceki at qos.ch noreply.ceki at qos.ch
Sat Aug 16 16:57:18 CEST 2008


Author: ceki
Date: Sat Aug 16 16:57:18 2008
New Revision: 1757

Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/BasicConfigurator.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/Configurator.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/JMSQueueSink.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/JMSTopicSink.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java
   logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticLoggerBinder.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java

Log:
- ContextInitializer now has a constructor taking a LoggerContext
  It's static methods have become instance methods with their 
  first argument (a LoggerContext) removed. This leads to very slightly
  simpler code.

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/BasicConfigurator.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/BasicConfigurator.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/BasicConfigurator.java	Sat Aug 16 16:57:18 2008
@@ -1,7 +1,7 @@
 /**
  * Logback: the generic, reliable, fast and flexible logging framework.
  * 
- * Copyright (C) 1999-2006, QOS.ch
+ * Copyright (C) 1999-2008, QOS.ch
  * 
  * This library is free software, you can redistribute it and/or modify it under
  * the terms of the GNU Lesser General Public License as published by the Free

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/Configurator.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/Configurator.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/jmx/Configurator.java	Sat Aug 16 16:57:18 2008
@@ -49,7 +49,7 @@
     addInfo("Shutting down context: " + lc.getName());
     lc.shutdownAndReset();
     try {
-      ContextInitializer.autoConfig(lc, lc.getClass().getClassLoader());
+      new ContextInitializer(lc).autoConfig(lc.getClass().getClassLoader());
       addInfo("Context: " + lc.getName() + " reloaded.");
     } catch(JoranException je) {
       addError("Reloading of context: " + lc.getName() + " failed.", je);
@@ -70,7 +70,7 @@
     LoggerContext lc = (LoggerContext) context;
     addInfo("Shutting down context: " + lc.getName());
     lc.shutdownAndReset();
-    ContextInitializer.configureByResource(lc, url);
+    new ContextInitializer(lc).configureByResource(url);
     addInfo("Context: " + lc.getName() + " reloaded.");
   }
 

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/JMSQueueSink.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/JMSQueueSink.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/JMSQueueSink.java	Sat Aug 16 16:57:18 2008
@@ -60,7 +60,7 @@
 
     LoggerContext loggerContext = (LoggerContext) LoggerFactory
         .getILoggerFactory();
-    ContextInitializer.autoConfig(loggerContext);
+    new ContextInitializer(loggerContext).autoConfig();
 
     new JMSQueueSink(qcfBindingName, queueBindingName, username, password);
 

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/JMSTopicSink.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/JMSTopicSink.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/JMSTopicSink.java	Sat Aug 16 16:57:18 2008
@@ -60,7 +60,7 @@
 
     LoggerContext loggerContext = (LoggerContext) LoggerFactory
         .getILoggerFactory();
-    ContextInitializer.autoConfig(loggerContext);
+    new ContextInitializer(loggerContext).autoConfig();
 
     new JMSTopicSink(tcfBindingName, topicBindingName, username, password);
 

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/ContextJNDISelector.java	Sat Aug 16 16:57:18 2008
@@ -95,7 +95,7 @@
           configureLoggerContextByResource(loggerContext, configFilePath);
         } else {
           try {
-            ContextInitializer.autoConfig(loggerContext);
+            new ContextInitializer(loggerContext).autoConfig();
           } catch(JoranException je) {
             StatusPrinter.print(loggerContext);
           }

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java	Sat Aug 16 16:57:18 2008
@@ -8,6 +8,8 @@
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.joran.JoranConfigurator;
 import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.status.InfoStatus;
+import ch.qos.logback.core.status.StatusManager;
 import ch.qos.logback.core.util.Loader;
 
 // contributors
@@ -24,8 +26,14 @@
   final public static String CONFIG_FILE_PROPERTY = "logback.configurationFile";
   final public static String STATUS_LISTENER_CLASS = "logback.statusListenerClass";
   final public static String SYSOUT = "SYSOUT";
-  
-  public static void configureByResource(LoggerContext loggerContext, URL url)
+
+  final LoggerContext loggerContext;
+
+  public ContextInitializer(LoggerContext loggerContext) {
+    this.loggerContext = loggerContext;
+  }
+
+  public void configureByResource(URL url)
       throws JoranException {
     if (url == null) {
       throw new IllegalArgumentException("URL argument cannot be null");
@@ -35,52 +43,68 @@
     configurator.doConfigure(url);
   }
 
-  static URL findConfigFileURLFromSystemProperties(ClassLoader classLoader) {
+  private URL findConfigFileURLFromSystemProperties(ClassLoader classLoader) {
     String logbackConfigFile = System.getProperty(CONFIG_FILE_PROPERTY, null);
     if (logbackConfigFile != null) {
+      URL result = null;
       try {
-        return new URL(logbackConfigFile);
+        result = new URL(logbackConfigFile);
+        return result;
       } catch (MalformedURLException e) {
         // so, resource is not a URL:
         // attempt to get the resource from the class path
-        URL urlAsResource = Loader.getResource(logbackConfigFile, classLoader);
-        if (urlAsResource != null) {
-          return urlAsResource;
+        result = Loader.getResource(logbackConfigFile, classLoader);
+        if (result != null) {
+          return result;
         }
         File f = new File(logbackConfigFile);
         if (f.exists() && f.isFile()) {
           try {
-            return f.toURL();
+            result = f.toURL();
+            return result;
           } catch (MalformedURLException e1) {
           }
         }
+      } finally {
+        statusOnResourceSearch(logbackConfigFile, result);
       }
     }
     return null;
   }
 
-  public static void autoConfig(LoggerContext loggerContext,
-      ClassLoader classLoader) throws JoranException {
+  public void autoConfig(ClassLoader classLoader) throws JoranException {
     StatusListenerConfigHelper.installIfAsked(loggerContext);
-    
+
     URL url = findConfigFileURLFromSystemProperties(classLoader);
     if (url == null) {
       url = Loader.getResource(TEST_AUTOCONFIG_FILE, classLoader);
+      statusOnResourceSearch(TEST_AUTOCONFIG_FILE, url);
     }
     if (url == null) {
       url = Loader.getResource(AUTOCONFIG_FILE, classLoader);
+      statusOnResourceSearch(AUTOCONFIG_FILE, url);
     }
     if (url != null) {
-      configureByResource(loggerContext, url);
+      configureByResource(url);
     } else {
       BasicConfigurator.configure(loggerContext);
     }
   }
 
-  public static void autoConfig(LoggerContext loggerContext)
-      throws JoranException {
+  public void autoConfig() throws JoranException {
     ClassLoader tccl = Loader.getTCL();
-    autoConfig(loggerContext, tccl);
+    autoConfig(tccl);
+  }
+
+  private void statusOnResourceSearch(String resourceName, URL url) {
+    StatusManager sm = loggerContext.getStatusManager();
+    if (url == null) {
+      sm.add(new InfoStatus("Could not find resource [" + resourceName + "]",
+          loggerContext));
+    } else {
+      sm.add(new InfoStatus("Found resource [" + resourceName + "]",
+          loggerContext));
+    }
   }
 
 }

Modified: logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticLoggerBinder.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticLoggerBinder.java	(original)
+++ logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticLoggerBinder.java	Sat Aug 16 16:57:18 2008
@@ -59,7 +59,7 @@
       LoggerContext defaultLoggerContext = new LoggerContext();
       defaultLoggerContext.setName("default");
       try {
-        ContextInitializer.autoConfig(defaultLoggerContext);
+        new ContextInitializer(defaultLoggerContext).autoConfig();
       } catch (JoranException je) {
         // TODO test me
         Util.reportFailure("Failed to auto configure default logger context",

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java	Sat Aug 16 16:57:18 2008
@@ -58,7 +58,7 @@
   // of users trying to follows the manual and logback-examples from an IDE
   public void testReset() throws JoranException {
     {
-      ContextInitializer.autoConfig(lc);
+      new ContextInitializer(lc).autoConfig();
       Appender appender = root.getAppender("STDOUT");
       assertNotNull(appender);
       assertTrue(appender instanceof ConsoleAppender);
@@ -81,7 +81,7 @@
   public void doAutoConfigFromSystemProperties(String val) throws JoranException {
     //lc.shutdownAndReset();
     System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, val);
-    ContextInitializer.autoConfig(lc);
+    new ContextInitializer(lc).autoConfig();
     Appender appender = root.getAppender("AUTO_BY_SYSTEM_PROPERTY");
     assertNotNull(appender);
   }


More information about the logback-dev mailing list