You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2013/10/17 15:40:28 UTC

svn commit: r1533079 - /sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogbackManager.java

Author: bdelacretaz
Date: Thu Oct 17 13:40:28 2013
New Revision: 1533079

URL: http://svn.apache.org/r1533079
Log:
SLING-3185 - avoid ClassCastException caused by slf4j not being fully initialized. Contributed by Chetan Mehrotra, thanks!

Modified:
    sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogbackManager.java

Modified: sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogbackManager.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogbackManager.java?rev=1533079&r1=1533078&r2=1533079&view=diff
==============================================================================
--- sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogbackManager.java (original)
+++ sling/trunk/bundles/commons/log/src/main/java/org/apache/sling/commons/log/logback/internal/LogbackManager.java Thu Oct 17 13:40:28 2013
@@ -43,7 +43,9 @@ import org.osgi.framework.ServiceFactory
 import org.osgi.framework.ServiceReference;
 import org.osgi.framework.ServiceRegistration;
 import org.osgi.util.tracker.ServiceTracker;
+import org.slf4j.ILoggerFactory;
 import org.slf4j.LoggerFactory;
+import org.slf4j.impl.StaticLoggerBinder;
 
 public class LogbackManager extends LoggerContextAwareBase {
     private static final String PREFIX = "org.apache.sling.commons.log";
@@ -64,7 +66,7 @@ public class LogbackManager extends Logg
 
     private final List<LogbackResetListener> resetListeners = new ArrayList<LogbackResetListener>();
 
-    private final org.slf4j.Logger log = LoggerFactory.getLogger(getClass());
+    private final org.slf4j.Logger log;
 
     /**
      * Acts as a bridge between Logback and OSGi
@@ -98,8 +100,9 @@ public class LogbackManager extends Logg
 
     public LogbackManager(BundleContext bundleContext) throws InvalidSyntaxException {
         final long startTime = System.currentTimeMillis();
+        ensureSlf4jIsInitialized();
         setLoggerContext((LoggerContext) LoggerFactory.getILoggerFactory());
-
+        this.log = LoggerFactory.getLogger(getClass());
         this.rootDir = getRootDir(bundleContext);
 
         this.debug = Boolean.parseBoolean(bundleContext.getProperty(DEBUG));
@@ -134,6 +137,17 @@ public class LogbackManager extends Logg
         started = true;
     }
 
+    private void ensureSlf4jIsInitialized() {
+        ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
+        //SLING-3185 Check if instance of LoggerContext as
+        //in case SLF4J has not completely initialized it would return a temp LoggerFactory
+        //SubstituteLoggerFactory
+        if(!(loggerFactory instanceof LoggerContext)){
+            //This ensures that Logger implementation is binded by the time call returns
+            StaticLoggerBinder.getSingleton();
+        }
+    }
+
     public void shutdown() {
         logConfigManager.close();
 
@@ -225,7 +239,7 @@ public class LogbackManager extends Logg
             success = true;
         } catch (Throwable t) {
             //Need to catch any error as Logback must work in all scenarios
-            addError("Error configuring Logback",t);
+            addError("Error configuring Logback", t);
         } finally {
             if(!success){
                 cb.fallbackConfiguration(eventList, createConfigurator(), statusListener);