[logback-dev] svn commit: r1360 - in logback/trunk/logback-classic/src: main/java/ch/qos/logback/classic/selector/servlet main/java/ch/qos/logback/classic/spi main/java/org/slf4j main/java/org/slf4j/impl test/java/ch/qos/logback/classic test/java/ch/qos/logback/classic/control test/java/ch/qos/logback/classic/net/mock test/java/ch/qos/logback/classic/selector

noreply.ceki at qos.ch noreply.ceki at qos.ch
Tue Feb 27 12:23:16 CET 2007


Author: ceki
Date: Tue Feb 27 12:23:16 2007
New Revision: 1360

Removed:
   logback/trunk/logback-classic/src/main/java/org/slf4j/LoggerFactory.java
   logback/trunk/logback-classic/src/main/java/org/slf4j/MarkerFactory.java
Modified:
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/ContextDetachingSCL.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java
   logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEventExt.java
   logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticLoggerBinder.java
   logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticMarkerBinder.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/HLogger.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/control/ControlLogger.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/mock/MockInitialContextFactory.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextDetachingSCLTest.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextJNDISelectorTest.java

Log:
Syncing with SLF4J 1.3.0:

 - Removed org.slf4j.LoggerFactory and migrated ContextSelector related initialization code
   to org.slf4j.impl.StaticLoggerBinder

 - Minor adjustments in test cases

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/ContextDetachingSCL.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/ContextDetachingSCL.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/ContextDetachingSCL.java	Tue Feb 27 12:23:16 2007
@@ -18,7 +18,7 @@
 import javax.servlet.ServletContextListener;
 
 import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import org.slf4j.impl.StaticLoggerBinder;
 
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.selector.ContextSelector;
@@ -26,7 +26,7 @@
 
 public class ContextDetachingSCL implements ServletContextListener {
 
-  public void contextDestroyed(ServletContextEvent arg0) {
+  public void contextDestroyed(ServletContextEvent servletContextEvent) {
     String loggerContextName = null;
     
     try {
@@ -38,7 +38,7 @@
     if (loggerContextName != null) {
       System.out.println("About to detach context named " + loggerContextName);
       
-      ContextSelector selector = LoggerFactory.getContextSelector();
+      ContextSelector selector = StaticLoggerBinder.SINGLETON.getContextSelector();
       LoggerContext context = selector.detachLoggerContext(loggerContextName);
       if (context != null) {
         Logger logger = context.getLogger(LoggerContext.ROOT_NAME);

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/selector/servlet/LoggerContextFilter.java	Tue Feb 27 12:23:16 2007
@@ -20,6 +20,7 @@
 import javax.servlet.ServletResponse;
 
 import org.slf4j.LoggerFactory;
+import org.slf4j.impl.StaticLoggerBinder;
 
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.selector.ContextJNDISelector;
@@ -56,7 +57,7 @@
       FilterChain chain) throws IOException, ServletException {
 
     LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
-    ContextSelector selector = LoggerFactory.getContextSelector();
+    ContextSelector selector = StaticLoggerBinder.SINGLETON.getContextSelector();
     ContextJNDISelector sel = null;
 
     if (selector instanceof ContextJNDISelector) {

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java	Tue Feb 27 12:23:16 2007
@@ -17,7 +17,7 @@
 import java.util.Map;
 
 import org.slf4j.Marker;
-import org.slf4j.impl.MessageFormatter;
+import org.slf4j.helpers.MessageFormatter;
 
 import ch.qos.logback.classic.Level;
 import ch.qos.logback.classic.Logger;

Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEventExt.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEventExt.java	(original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEventExt.java	Tue Feb 27 12:23:16 2007
@@ -18,7 +18,7 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.Marker;
-import org.slf4j.impl.MessageFormatter;
+import org.slf4j.helpers.MessageFormatter;
 
 import ch.qos.logback.classic.Level;
 

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 Feb 27 12:23:16 2007
@@ -11,9 +11,16 @@
 
 import org.slf4j.ILoggerFactory;
 import org.slf4j.LoggerFactory;
+import org.slf4j.helpers.Util;
 import org.slf4j.spi.LoggerFactoryBinder;
 
+import ch.qos.logback.classic.ClassicGlobal;
 import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.selector.ContextJNDISelector;
+import ch.qos.logback.classic.selector.ContextSelector;
+import ch.qos.logback.classic.selector.DefaultContextSelector;
+import ch.qos.logback.classic.util.ContextInitializer;
+import ch.qos.logback.core.util.OptionHelper;
 
 /**
  * 
@@ -24,30 +31,61 @@
  */
 public class StaticLoggerBinder implements LoggerFactoryBinder {
 
+  private ContextSelector contextSelector;
+
   /**
    * The unique instance of this class.
    */
   public static final StaticLoggerBinder SINGLETON = new StaticLoggerBinder();
-  private static final String loggerFactoryClassStr = LoggerContext.class
+  private static final String loggerFactoryClassStr = ContextSelector.class
       .getName();
 
-  /**
-   * The ILoggerFactory instance returned by the {@link #getLoggerFactory}
-   * method should always be the same object
-   */
-  private final ILoggerFactory loggerFactory;
-
   private StaticLoggerBinder() {
-    LoggerContext lc = new LoggerContext();
-    lc.setName("default");
-    loggerFactory = lc;
+    initialize();
+  }
+
+  public void initialize() {
+    try {
+      // let's configure a default context
+      LoggerContext defaultLoggerContext = new LoggerContext();
+      defaultLoggerContext.setName("default");
+      ContextInitializer.autoConfig(defaultLoggerContext);
+
+      // See if a special context selector is needed
+      String contextSelectorStr = OptionHelper.getSystemProperty(
+          ClassicGlobal.LOGBACK_CONTEXT_SELECTOR, null);
+      if (contextSelectorStr == null) {
+        contextSelector = new DefaultContextSelector(defaultLoggerContext);
+      } else if (contextSelectorStr.equals("JNDI")) {
+        // if jndi is specified, let's use the appropriate class
+        contextSelector = new ContextJNDISelector(defaultLoggerContext);
+      }
+    } catch (Exception e) {
+      // we should never get here
+      Util.reportFailure("Failed to instantiate ["
+          + LoggerContext.class.getName() + "]", e);
+    }
   }
 
   public ILoggerFactory getLoggerFactory() {
-    return loggerFactory;
+    if (contextSelector == null) {
+      throw new IllegalStateException(
+          "contextSelector cannot be null. See also http://logback.qos.ch/codes.html#null_CS");
+    }
+    return contextSelector.getLoggerContext();
   }
 
   public String getLoggerFactoryClassStr() {
     return loggerFactoryClassStr;
   }
+
+  /**
+   * Return the {@link ContextSelector} instance in use.
+   * 
+   * @return the ContextSelector instance in use
+   */
+  public ContextSelector getContextSelector() {
+    return contextSelector;
+  }
+
 }

Modified: logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticMarkerBinder.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticMarkerBinder.java	(original)
+++ logback/trunk/logback-classic/src/main/java/org/slf4j/impl/StaticMarkerBinder.java	Tue Feb 27 12:23:16 2007
@@ -35,6 +35,7 @@
 
 import org.slf4j.IMarkerFactory;
 import org.slf4j.MarkerFactory;
+import org.slf4j.helpers.BasicMarkerFactory;
 import org.slf4j.spi.MarkerFactoryBinder;
 
 /**

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/HLogger.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/HLogger.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/HLogger.java	Tue Feb 27 12:23:16 2007
@@ -15,7 +15,7 @@
 import java.util.Iterator;
 import java.util.Map;
 
-import org.slf4j.impl.MarkerIgnoringBase;
+import org.slf4j.helpers.MarkerIgnoringBase;
 
 import ch.qos.logback.classic.ClassicGlobal;
 import ch.qos.logback.classic.Level;

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/control/ControlLogger.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/control/ControlLogger.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/control/ControlLogger.java	Tue Feb 27 12:23:16 2007
@@ -9,7 +9,7 @@
  */
 package ch.qos.logback.classic.control;
 
-import org.slf4j.impl.MarkerIgnoringBase;
+import org.slf4j.helpers.MarkerIgnoringBase;
 
 import ch.qos.logback.classic.Level;
 

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/mock/MockInitialContextFactory.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/mock/MockInitialContextFactory.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/mock/MockInitialContextFactory.java	Tue Feb 27 12:23:16 2007
@@ -10,14 +10,18 @@
   static MockInitialContext mic;
 
   static {
+    System.out.println("MockInitialContextFactory static called");
+    initialize();
+  }
+ 
+  public static void initialize() {
     try {
       mic = new MockInitialContext();
     } catch (NamingException e) {
       e.printStackTrace();
     }
-
   }
-
+  
   public Context getInitialContext(Hashtable<?, ?> environment)
       throws NamingException {
     return mic;

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextDetachingSCLTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextDetachingSCLTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextDetachingSCLTest.java	Tue Feb 27 12:23:16 2007
@@ -2,8 +2,8 @@
 
 import junit.framework.TestCase;
 
-import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.slf4j.impl.StaticLoggerBinder;
 
 import ch.qos.logback.classic.ClassicGlobal;
 import ch.qos.logback.classic.net.mock.MockInitialContext;
@@ -19,10 +19,11 @@
   @Override
   protected void setUp() throws Exception {
     System.setProperty(ClassicGlobal.LOGBACK_CONTEXT_SELECTOR, "JNDI");
-    LoggerFactory.setup();
+    //LoggerFactory.setup();
     
     listener = new ContextDetachingSCL();
     
+    MockInitialContextFactory.initialize();
     MockInitialContext mic = MockInitialContextFactory.getContext();
     mic.map.put(ClassicGlobal.JNDI_CONTEXT_NAME, "toto");
     
@@ -30,8 +31,7 @@
     System.setProperty(INITIAL_CONTEXT_KEY, MockInitialContextFactory.class.getName());
     
     //this call will create the context "toto"
-    @SuppressWarnings("unused")
-    Logger logger = LoggerFactory.getLogger(ContextDetachingSCLTest.class);
+    LoggerFactory.getLogger(ContextDetachingSCLTest.class);
 
     super.setUp();
   }
@@ -43,19 +43,21 @@
   }
   
   public void testDetach() {
-    ContextJNDISelector selector = (ContextJNDISelector) LoggerFactory.getContextSelector();
+    ContextJNDISelector selector = (ContextJNDISelector) StaticLoggerBinder.SINGLETON.getContextSelector();
     listener.contextDestroyed(null);
-    
     assertEquals(0, selector.getCount());
   }
   
   public void testDetachWithMissingContext() {
     MockInitialContext mic = MockInitialContextFactory.getContext();
     mic.map.put(ClassicGlobal.JNDI_CONTEXT_NAME, "tata");
-    
-    ContextJNDISelector selector = (ContextJNDISelector) LoggerFactory.getContextSelector();
+    ContextJNDISelector selector = (ContextJNDISelector) StaticLoggerBinder.SINGLETON.getContextSelector();
+    assertEquals("tata", selector.getLoggerContext().getName());
+
+    mic.map.put(ClassicGlobal.JNDI_CONTEXT_NAME, "titi");
+    assertEquals("titi", selector.getLoggerContext().getName());
     listener.contextDestroyed(null);
-    
+
     assertEquals(1, selector.getCount());
   }
   

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextJNDISelectorTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextJNDISelectorTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/selector/ContextJNDISelectorTest.java	Tue Feb 27 12:23:16 2007
@@ -2,28 +2,26 @@
 
 import junit.framework.TestCase;
 
-import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.slf4j.impl.StaticLoggerBinder;
 
 import ch.qos.logback.classic.ClassicGlobal;
 import ch.qos.logback.classic.net.mock.MockInitialContext;
 import ch.qos.logback.classic.net.mock.MockInitialContextFactory;
-import ch.qos.logback.classic.selector.servlet.ContextDetachingSCL;
 import ch.qos.logback.core.Context;
 
 public class ContextJNDISelectorTest extends TestCase {
   
   static String INITIAL_CONTEXT_KEY = "java.naming.factory.initial";
 
-  ContextDetachingSCL listener;
-  
   @Override
   protected void setUp() throws Exception {    
-    System.setProperty(ClassicGlobal.LOGBACK_CONTEXT_SELECTOR, "JNDI");
-    LoggerFactory.setup();
+    super.setUp();
     
-    listener = new ContextDetachingSCL();
+    System.setProperty(ClassicGlobal.LOGBACK_CONTEXT_SELECTOR, "JNDI");
+    StaticLoggerBinder.SINGLETON.initialize();
     
+    MockInitialContextFactory.initialize();
     MockInitialContext mic = MockInitialContextFactory.getContext();
     mic.map.put(ClassicGlobal.JNDI_CONTEXT_NAME, "toto");
     
@@ -31,10 +29,7 @@
     System.setProperty(INITIAL_CONTEXT_KEY, MockInitialContextFactory.class.getName());
     
     //this call will create the context "toto"
-    @SuppressWarnings("unused")
-    Logger logger = LoggerFactory.getLogger(ContextDetachingSCLTest.class);
-
-    super.setUp();
+    LoggerFactory.getLogger(ContextDetachingSCLTest.class);
   }
 
   @Override
@@ -44,7 +39,7 @@
   }
 
   public void testGetExistingContext() {
-    ContextSelector selector = LoggerFactory.getContextSelector();
+    ContextSelector selector = StaticLoggerBinder.SINGLETON.getContextSelector();
     Context context = selector.getLoggerContext();
     assertEquals("toto", context.getName());
   }
@@ -53,20 +48,19 @@
     MockInitialContext mic = MockInitialContextFactory.getContext();
     mic.map.put(ClassicGlobal.JNDI_CONTEXT_NAME, "tata");
     
-    @SuppressWarnings("unused")
-    Logger logger = LoggerFactory.getLogger(ContextDetachingSCLTest.class);
+    LoggerFactory.getLogger(ContextDetachingSCLTest.class);
     
-    ContextJNDISelector selector = (ContextJNDISelector)LoggerFactory.getContextSelector();
+    ContextJNDISelector selector = (ContextJNDISelector)StaticLoggerBinder.SINGLETON.getContextSelector();
     Context context = selector.getLoggerContext();
     assertEquals("tata", context.getName());
-    assertEquals(2, selector.getCount());
+    assertEquals(1, selector.getCount());
   }
   
   public void testReturnDefaultContext() {
     MockInitialContext mic = MockInitialContextFactory.getContext();
     mic.map.put(ClassicGlobal.JNDI_CONTEXT_NAME, null);
 
-    ContextJNDISelector selector = (ContextJNDISelector)LoggerFactory.getContextSelector();
+    ContextJNDISelector selector = (ContextJNDISelector)StaticLoggerBinder.SINGLETON.getContextSelector();
     Context context = selector.getLoggerContext();
     
     assertEquals("default", context.getName());    



More information about the logback-dev mailing list