[slf4j-dev] svn commit: r1192 - in slf4j/trunk: slf4j-api/src/main/java/org/slf4j slf4j-api/src/main/java/org/slf4j/helpers slf4j-nop/src/main/java/org/slf4j/impl slf4j-site/src/site/pages

ceki at slf4j.org ceki at slf4j.org
Thu Oct 16 14:49:06 CEST 2008


Author: ceki
Date: Thu Oct 16 14:49:05 2008
New Revision: 1192

Added:
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/SubstituteLoggerFactory.java
   slf4j/trunk/slf4j-nop/src/main/java/org/slf4j/impl/NOPLoggerFactory.java
      - copied, changed from r1190, /slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NOPLoggerFactory.java
Removed:
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NOPLoggerFactory.java
Modified:
   slf4j/trunk/slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
   slf4j/trunk/slf4j-nop/src/main/java/org/slf4j/impl/StaticLoggerBinder.java
   slf4j/trunk/slf4j-site/src/site/pages/codes.html

Log:
Further refinements in relation to bug 106.

Since the logger returned by the nop factory are not functional (nop loggers), 
we need to tell the user that she should not expect any logging from such loggers.

LoggerFactory will now list any nop loggers that it had to create. 


Modified: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/LoggerFactory.java
==============================================================================
--- slf4j/trunk/slf4j-api/src/main/java/org/slf4j/LoggerFactory.java	(original)
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/LoggerFactory.java	Thu Oct 16 14:49:05 2008
@@ -24,7 +24,10 @@
 
 package org.slf4j;
 
-import org.slf4j.helpers.NOPLoggerFactory;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.slf4j.helpers.SubstituteLoggerFactory;
 import org.slf4j.helpers.Util;
 import org.slf4j.impl.StaticLoggerBinder;
 
@@ -51,6 +54,7 @@
   static final String NO_STATICLOGGERBINDER_URL = "http://www.slf4j.org/codes.html#StaticLoggerBinder";
   static final String NULL_LF_URL = "http://www.slf4j.org/codes.html#null_LF";
   static final String VERSION_MISMATCH = "http://www.slf4j.org/codes.html#version_mismatch";
+  static final String SUBSTITUTE_LOGGER_URL = "http://www.slf4j.org/codes.html#substituteLogger";
 
   static private final String EXPECTED_VERSION = "1.5.4-SNAPSHOT";
 
@@ -65,10 +69,12 @@
 
   private final static void staticInitialize() {
     try {
-      // support re-entrant behavior. 
+      // support re-entrant behavior.
       // See also http://bugzilla.slf4j.org/show_bug.cgi?id=106
-      loggerFactory = new NOPLoggerFactory();
+      List loggerNameList = new ArrayList();
+      loggerFactory = new SubstituteLoggerFactory(loggerNameList);
       loggerFactory = StaticLoggerBinder.SINGLETON.getLoggerFactory();
+      emitSubstitureLoggerWarning(loggerNameList);
     } catch (NoClassDefFoundError ncde) {
       loggerFactory = null; // undo NOPLoggerFactory
       String msg = ncde.getMessage();
@@ -88,6 +94,21 @@
     }
   }
 
+  private final static void emitSubstitureLoggerWarning(List loggerNameList) {
+    if(loggerNameList.size() == 0) {
+      return;
+    }
+    Util
+        .reportFailure("The following loggers will not work becasue they were created");
+    Util
+        .reportFailure("during the default configuration phase of the underlying logging system.");
+    Util.reportFailure("See also " + SUBSTITUTE_LOGGER_URL);
+    for (int i = 0; i < loggerNameList.size(); i++) {
+      String loggerName = (String) loggerNameList.get(i);
+      Util.reportFailure(loggerName);
+    }
+  }
+
   private final static void versionSanityCheck() {
     try {
       String actualVer = StaticLoggerBinder.VERSION;
@@ -108,7 +129,6 @@
       e.printStackTrace();
     }
   }
-  
 
   /**
    * Return a logger named according to the name parameter using the statically

Added: slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/SubstituteLoggerFactory.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/SubstituteLoggerFactory.java	Thu Oct 16 14:49:05 2008
@@ -0,0 +1,67 @@
+/* 
+ * Copyright (c) 2004-2005 SLF4J.ORG
+ * Copyright (c) 2004-2005 QOS.ch
+ *
+ * All rights reserved.
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to  deal in  the Software without  restriction, including
+ * without limitation  the rights to  use, copy, modify,  merge, publish,
+ * distribute, and/or sell copies of  the Software, and to permit persons
+ * to whom  the Software is furnished  to do so, provided  that the above
+ * copyright notice(s) and this permission notice appear in all copies of
+ * the  Software and  that both  the above  copyright notice(s)  and this
+ * permission notice appear in supporting documentation.
+ * 
+ * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
+ * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT
+ * OF  THIRD PARTY  RIGHTS. IN  NO EVENT  SHALL THE  COPYRIGHT  HOLDER OR
+ * HOLDERS  INCLUDED IN  THIS  NOTICE BE  LIABLE  FOR ANY  CLAIM, OR  ANY
+ * SPECIAL INDIRECT  OR CONSEQUENTIAL DAMAGES, OR  ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS  OF USE, DATA OR PROFITS, WHETHER  IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE  OR OTHER TORTIOUS  ACTION, ARISING OUT OF  OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ * 
+ * Except as  contained in  this notice, the  name of a  copyright holder
+ * shall not be used in advertising or otherwise to promote the sale, use
+ * or other dealings in this Software without prior written authorization
+ * of the copyright holder.
+ *
+ */
+
+package org.slf4j.helpers;
+
+import java.util.List;
+
+import org.slf4j.ILoggerFactory;
+import org.slf4j.Logger;
+
+
+/**
+ * SubstituteLoggerFactory is an trivial implementation of {@link
+ * ILoggerFactory} which always returns the unique instance of
+ * NOPLogger.
+ * 
+ * <p>It used as a temporary substitute for the real ILoggerFactory
+ * during its auto-configuration which may re-enter LoggerFactory
+ * to obtain logger instances. See also  
+ * http://bugzilla.slf4j.org/show_bug.cgi?id=106
+ * 
+ * @author Ceki G&uuml;lc&uuml;
+ */
+public class SubstituteLoggerFactory implements ILoggerFactory {
+  
+  // keep a record of requested logger names
+  final List loggerNameList;
+  
+  public SubstituteLoggerFactory(List loggerNameList) {
+    this.loggerNameList = loggerNameList;
+  }
+  
+  public Logger getLogger(String name) {
+    loggerNameList.add(name);
+    return NOPLogger.NOP_LOGGER;
+  }
+}

Copied: slf4j/trunk/slf4j-nop/src/main/java/org/slf4j/impl/NOPLoggerFactory.java (from r1190, /slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NOPLoggerFactory.java)
==============================================================================
--- /slf4j/trunk/slf4j-api/src/main/java/org/slf4j/helpers/NOPLoggerFactory.java	(original)
+++ slf4j/trunk/slf4j-nop/src/main/java/org/slf4j/impl/NOPLoggerFactory.java	Thu Oct 16 14:49:05 2008
@@ -31,7 +31,7 @@
  *
  */
 
-package org.slf4j.helpers;
+package org.slf4j.impl;
 
 import org.slf4j.ILoggerFactory;
 import org.slf4j.Logger;

Modified: slf4j/trunk/slf4j-nop/src/main/java/org/slf4j/impl/StaticLoggerBinder.java
==============================================================================
--- slf4j/trunk/slf4j-nop/src/main/java/org/slf4j/impl/StaticLoggerBinder.java	(original)
+++ slf4j/trunk/slf4j-nop/src/main/java/org/slf4j/impl/StaticLoggerBinder.java	Thu Oct 16 14:49:05 2008
@@ -35,7 +35,6 @@
 
 import org.slf4j.ILoggerFactory;
 import org.slf4j.LoggerFactory;
-import org.slf4j.helpers.NOPLoggerFactory;
 import org.slf4j.spi.LoggerFactoryBinder;
 
 /**

Modified: slf4j/trunk/slf4j-site/src/site/pages/codes.html
==============================================================================
--- slf4j/trunk/slf4j-site/src/site/pages/codes.html	(original)
+++ slf4j/trunk/slf4j-site/src/site/pages/codes.html	Thu Oct 16 14:49:05 2008
@@ -177,6 +177,47 @@
     mismatch problem, it emits a warning about the said mismatch.
     </p>
 
+
+  <h3><a name="substituteLogger" href="#substituteLogger">Substiture
+  loggers were created during the default configuration phase of the
+  underlying logging system</a></h3>
+
+  <p>Highly configurable logging systems such as logback and log4j may
+  create components which invoke loggers during their own
+  initialization.  See issue <a
+  href="http://jira.qos.ch/browse/LBCORE-47">lbcore-47</a> for a
+  typical occurence. However, since the binding process with SLF4J has
+  not yet completed (because the underlying logging system was not yet
+  completely loaded into memory), it is not possible to honor such
+  logger creation requests, resulting in a
+  <code>NullPointerException</code>.</p>
+
+  <p>To avoid this chicken-and-egg problem, SLF4J substitutes a
+  no-operation logger factory during this initialization
+  phase. However, the loggers returned during this phase by the
+  substitute logger factory are not operational. They are nop
+  implementations.
+  </p>
+  
+  <p>If any substitute logger had to be created, SLF4J will emit a
+  warning listing such nop loggers. This warning is intended to let
+  you know that you should not expect any logging output from these
+  loggers.
+  </p>
+
+  <p>The only way to obtain output from the listed loggers, is to
+  isolate the components invoking these loggers and to exlude them
+  from the default configuration. Both logback and log4j allow
+  multi-step configuration. It follows that the problematic components
+  should be configured in a second step separate from default
+  configuration.
+  </p>
+
+  <p>If you are not interested in the output from any of the
+  substiture loggers, then no action is required on your part.</p>
+
+
+
 </div>
 </body>
 </html>



More information about the slf4j-dev mailing list