[logback-dev] svn commit: r1723 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/util test/input test/java/ch/qos/logback/classic/util test/resources

noreply.ceki at qos.ch noreply.ceki at qos.ch
Fri Jul 18 23:44:49 CEST 2008


Author: ceki
Date: Fri Jul 18 23:44:49 2008
New Revision: 1723

Added:
   logback/trunk/logback-classic/src/test/input/autoConfig.xml
   logback/trunk/logback-classic/src/test/resources/autoConfigAsResource.xml
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/util/InitializationTest.java

Log:

- Related to LBCORE-32

  added support for passing the configuration file as a system property.
  The system property name is "logback.configurationFile". Values can be 
  of type URL, a file, or a resource name.

  I am guessing that this modification should satisfy the requirements
  for most users who voted on this issue. I am not too keen on adding
  other config directives as system properties as suggested by M. Fowles.


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	Fri Jul 18 23:44:49 2008
@@ -1,5 +1,7 @@
 package ch.qos.logback.classic.util;
 
+import java.io.File;
+import java.net.MalformedURLException;
 import java.net.URL;
 
 import ch.qos.logback.classic.BasicConfigurator;
@@ -8,10 +10,18 @@
 import ch.qos.logback.core.joran.spi.JoranException;
 import ch.qos.logback.core.util.Loader;
 
+// contributors
+// Ted Graham, Matt Fowles, see also http://jira.qos.ch/browse/LBCORE-32
+/**
+ * This class contains logback's logic for automatic configuration
+ * 
+ * @author Ceki Gulcu
+ */
 public class ContextInitializer {
 
   final public static String AUTOCONFIG_FILE = "logback.xml";
   final public static String TEST_AUTOCONFIG_FILE = "logback-test.xml";
+  final public static String CONFIG_FILE_PROPERTY = "logback.configurationFile";
 
   public static void configureByResource(LoggerContext loggerContext, URL url)
       throws JoranException {
@@ -23,13 +33,39 @@
     configurator.doConfigure(url);
   }
 
+  static URL findConfigFileURLFromSystemProperties(ClassLoader classLoader) {
+    String logbackConfigFile = System.getProperty(CONFIG_FILE_PROPERTY, null);
+    if (logbackConfigFile != null) {
+      try {
+        return new URL(logbackConfigFile);
+      } 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;
+        }
+        File f = new File(logbackConfigFile);
+        if (f.exists() && f.isFile()) {
+          try {
+            return f.toURL();
+          } catch (MalformedURLException e1) {
+          }
+        }
+      }
+    }
+    return null;
+  }
+
   public static void autoConfig(LoggerContext loggerContext,
       ClassLoader classLoader) throws JoranException {
-    URL url = Loader.getResource(TEST_AUTOCONFIG_FILE, classLoader);
+    URL url = findConfigFileURLFromSystemProperties(classLoader);
+    if (url == null) {
+      url = Loader.getResource(TEST_AUTOCONFIG_FILE, classLoader);
+    }
     if (url == null) {
       url = Loader.getResource(AUTOCONFIG_FILE, classLoader);
     }
-
     if (url != null) {
       configureByResource(loggerContext, url);
     } else {

Added: logback/trunk/logback-classic/src/test/input/autoConfig.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/input/autoConfig.xml	Fri Jul 18 23:44:49 2008
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<configuration>
+
+	<appender name="AUTO_BY_SYSTEM_PROPERTY" class="ch.qos.logback.core.ConsoleAppender">
+		<layout class="ch.qos.logback.classic.PatternLayout">
+			<pattern>%msg%n"</pattern>
+		</layout>
+	</appender>
+
+	<root>
+		<level value="debug" />
+		<appender-ref ref="AUTO_BY_SYSTEM_PROPERTY" />
+	</root>
+</configuration>

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	Fri Jul 18 23:44:49 2008
@@ -8,6 +8,7 @@
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.core.Appender;
 import ch.qos.logback.core.ConsoleAppender;
+import ch.qos.logback.core.joran.spi.JoranException;
 
 public class InitializationTest extends TestCase {
 
@@ -23,6 +24,7 @@
 
   protected void tearDown() throws Exception {
     super.tearDown();
+    System.clearProperty(ContextInitializer.CONFIG_FILE_PROPERTY);
   }
 
   public void testAutoconfig() {
@@ -42,6 +44,19 @@
       Appender appender = root.getAppender("STDOUT");
       assertNull(appender);
     }
+  }
 
+  public void testAutoConfigFromSystemProperties() throws JoranException  {
+    doAutoConfigFromSystemProperties(TeztConstants.TEST_DIR_PREFIX + "input/autoConfig.xml");
+    doAutoConfigFromSystemProperties("autoConfigAsResource.xml");
+    // test passing a URL. note the relative path syntax with file:src/test/...
+    doAutoConfigFromSystemProperties("file:"+TeztConstants.TEST_DIR_PREFIX + "input/autoConfig.xml"); 
+  }
+  public void doAutoConfigFromSystemProperties(String val) throws JoranException {
+    lc.shutdownAndReset();
+    System.setProperty(ContextInitializer.CONFIG_FILE_PROPERTY, val);
+    ContextInitializer.autoConfig(lc);
+    Appender appender = root.getAppender("AUTO_BY_SYSTEM_PROPERTY");
+    assertNotNull(appender);
   }
 }

Added: logback/trunk/logback-classic/src/test/resources/autoConfigAsResource.xml
==============================================================================
--- (empty file)
+++ logback/trunk/logback-classic/src/test/resources/autoConfigAsResource.xml	Fri Jul 18 23:44:49 2008
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<configuration>
+
+	<appender name="AUTO_BY_SYSTEM_PROPERTY" class="ch.qos.logback.core.ConsoleAppender">
+		<layout class="ch.qos.logback.classic.PatternLayout">
+			<pattern>%msg%n"</pattern>
+		</layout>
+	</appender>
+
+	<root>
+		<level value="debug" />
+		<appender-ref ref="AUTO_BY_SYSTEM_PROPERTY" />
+
+	</root>
+</configuration>


More information about the logback-dev mailing list