[logback-dev] svn commit: r1593 - logback/trunk/logback-classic/src/main/java/org/slf4j/impl

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue Sep 18 22:43:32 CEST 2007


Author: ceki
Date: Tue Sep 18 22:43:32 2007
New Revision: 1593

Modified:
   logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticLoggerBinder.java

Log:
Allowing the user to specify a custom ContextSelector as requested in bug 96.

http://bugzilla.qos.ch/show_bug.cgi?id=96


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	Tue Sep 18 22:43:32 2007
@@ -9,6 +9,9 @@
  */
 package org.slf4j.impl;
 
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+
 import org.slf4j.ILoggerFactory;
 import org.slf4j.LoggerFactory;
 import org.slf4j.helpers.Util;
@@ -22,6 +25,7 @@
 import ch.qos.logback.classic.util.ContextInitializer;
 import ch.qos.logback.core.CoreGlobal;
 import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.util.Loader;
 import ch.qos.logback.core.util.OptionHelper;
 import ch.qos.logback.core.util.StatusPrinter;
 
@@ -70,6 +74,9 @@
       } else if (contextSelectorStr.equals("JNDI")) {
         // if jndi is specified, let's use the appropriate class
         contextSelector = new ContextJNDISelector(defaultLoggerContext);
+      } else {
+        contextSelector = dynamicalContextSelector(defaultLoggerContext,
+            contextSelectorStr);
       }
     } catch (Throwable t) {
       // we should never get here
@@ -78,6 +85,14 @@
     }
   }
 
+  static ContextSelector dynamicalContextSelector(
+      LoggerContext defaultLoggerContext, String contextSelectorStr) throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
+    Class contextSelectorClass = Loader.loadClass(contextSelectorStr);
+    Constructor cons = contextSelectorClass
+        .getConstructor(new Class[] { LoggerContext.class });
+    return (ContextSelector) cons.newInstance(defaultLoggerContext);
+  }
+
   public ILoggerFactory getLoggerFactory() {
     if (contextSelector == null) {
       throw new IllegalStateException(



More information about the logback-dev mailing list