[slf4j-dev] svn commit: r79 - in slf4j/trunk: . src/filtered-java/org/slf4j src/filtered-java/org/slf4j/impl tests tests/src/java/org/slf4j tests/src/java/org/slf4j/impl

ceki at slf4j.org ceki at slf4j.org
Fri May 20 17:48:10 CEST 2005


Author: ceki
Date: Fri May 20 17:48:07 2005
New Revision: 79

Added:
   slf4j/trunk/build.properties.sample
   slf4j/trunk/src/filtered-java/org/slf4j/Constants.java
   slf4j/trunk/src/filtered-java/org/slf4j/impl/package.html
   slf4j/trunk/tests/src/java/org/slf4j/SimpleLoggerFAFactory.java
   slf4j/trunk/tests/src/java/org/slf4j/XLoggerBySystemPropsTest.java
   slf4j/trunk/tests/src/java/org/slf4j/XLoggerFAFactory.java
   slf4j/trunk/tests/src/java/org/slf4j/impl/XLogger.java
   slf4j/trunk/tests/src/java/org/slf4j/impl/XLoggerFA.java
Modified:
   slf4j/trunk/src/filtered-java/org/slf4j/LoggerFactory.java
   slf4j/trunk/tests/build.xml
Log:

Added the capability to set the LoggerFactoryAdapter from the System properties.



Added: slf4j/trunk/build.properties.sample
==============================================================================
--- (empty file)
+++ slf4j/trunk/build.properties.sample	Fri May 20 17:48:07 2005
@@ -0,0 +1,3 @@
+
+slf4j-site=../site
+javadoc.dest=${slf4j-site}/docs/api
\ No newline at end of file

Added: slf4j/trunk/src/filtered-java/org/slf4j/Constants.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/src/filtered-java/org/slf4j/Constants.java	Fri May 20 17:48:07 2005
@@ -0,0 +1,8 @@
+
+package org.slf4j;
+
+public interface Constants {
+	
+	final public static String LOGGER_FA_FACTORY = "LOGGER_FA_FACTORY";
+	final public static String FA_FACTORY_METHOD_NAME = "getInstance";
+}

Modified: slf4j/trunk/src/filtered-java/org/slf4j/LoggerFactory.java
==============================================================================
--- slf4j/trunk/src/filtered-java/org/slf4j/LoggerFactory.java	(original)
+++ slf4j/trunk/src/filtered-java/org/slf4j/LoggerFactory.java	Fri May 20 17:48:07 2005
@@ -1,8 +1,12 @@
-/* 
+/**
+ * Copyright QOS.CH
+ */
+
+/*
  * Copyright (c) 2004-2005 SLF4J.ORG
  *
  * 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
@@ -12,7 +16,7 @@
  * 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
@@ -22,16 +26,16 @@
  * 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;
 
+
 // WARNING
 // WARNING Modifications MUST be made to the original file found at
 // WARNING $SLF4J_HOME/src/filtered-java/org/slf4j/LoggerFactory.java
@@ -49,21 +53,75 @@
   static LoggerFactoryAdapter adapter;
 
   // 
-  // WARNING Modify the original in
+  // WARNING Do not modify copies but the original in
   //         $SLF4J_HOME/src/filtered-java/org/slf4j/
-  
+  //
   static {
     String adapterClassStr = "org.slf4j.impl. at IMPL@LoggerFA";
-    System.out.println("SLF4J built for "+adapterClassStr);
+    System.out.println("SLF4J built for " + adapterClassStr);
+
+    adapter = getFactoryAdapterFromSystemProperties();
+
+    // if could not get an adapter from the system properties,  bind statically
+    if (adapter != null) {
+       System.out.println("However, SLF4J will use ["+adapter.getClass().getName()
+       		+ "] adapter from system properties.");
+    } else {
+      try {
+        adapter = new org.slf4j.impl. at IMPL@LoggerFA();
+      } catch (Exception e) {
+        // unless there was a problem with the build or the JVM we will never
+        // get exceptions
+        reportFailure(
+          "Could not instantiate instance of class [" + adapterClassStr + "]",
+          e);
+      }
+    }
+  }
+
+  /**
+   * Fetch the appropriate adapter as intructed by the system propties.
+   * 
+   * @return The appropriate LoggerFactoryAdapter as directed from the 
+   * system properties
+   */
+  private static LoggerFactoryAdapter getFactoryAdapterFromSystemProperties() {
+    String faFactoryClassName = null;
+
     try {
-      adapter = new org.slf4j.impl. at IMPL@LoggerFA();
-    } catch (Exception e) {
-      // unless there was a problem with the build or the JVM we will never
-      // get exceptions
-      System.err.println(
-        "Could not instantiate instance of class [" + adapterClassStr + "]");
-      e.printStackTrace();
+      faFactoryClassName = System.getProperty(Constants.LOGGER_FA_FACTORY);
+      if (faFactoryClassName == null) {
+        return null;
+      }
+
+      Class faFactoryClass = Class.forName(faFactoryClassName);
+      Class[] EMPTY_CLASS_ARRAY = {  };
+      java.lang.reflect.Method faFactoryMethod =
+        faFactoryClass.getDeclaredMethod(
+          Constants.FA_FACTORY_METHOD_NAME, EMPTY_CLASS_ARRAY);
+      LoggerFactoryAdapter adapter =
+        (LoggerFactoryAdapter) faFactoryMethod.invoke(null, null);
+      return adapter;
+    } catch (Throwable t) {
+      if (faFactoryClassName == null) {
+        reportFailure(
+          "Failed to fetch " + Constants.LOGGER_FA_FACTORY
+          + " system property.", t);
+      } else {
+        reportFailure(
+          "Failed to fectch LoggerFactoryAdapter using the "
+          + faFactoryClassName + " class.", t);
+      }
     }
+
+    // we could not get an adapter
+    return null;
+  }
+
+  static void reportFailure(String msg, Throwable t) {
+    System.out.println(msg);
+    System.out.println("Reported exception follows.");
+    t.printStackTrace(System.out);
   }
 
   public static Logger getLogger(String name) {

Added: slf4j/trunk/src/filtered-java/org/slf4j/impl/package.html
==============================================================================
--- (empty file)
+++ slf4j/trunk/src/filtered-java/org/slf4j/impl/package.html	Fri May 20 17:48:07 2005
@@ -0,0 +1,17 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+
+
+<html> 
+  <head>
+    <title></title>
+  </head>
+
+
+  <body>
+    
+    <p>Implementations of core logging interfaces defined in the {@link 
+    org.slf4j} package.</p>
+    
+    <hr/>
+  </body> 
+</html>

Modified: slf4j/trunk/tests/build.xml
==============================================================================
--- slf4j/trunk/tests/build.xml	(original)
+++ slf4j/trunk/tests/build.xml	Fri May 20 17:48:07 2005
@@ -144,9 +144,7 @@
 
 
   <target name="InvokeNLOG4J1212" depends="build, cleanOutputDir">
-    
     <copy file="input/nlog4j1212/basic.xml" tofile="${tests.javac.dest}/log4j.xml"/>
-
     <junit printsummary="yes" fork="yes" haltonfailure="yes">
       <classpath refid="nlog4j1212.classpath"/>
       <formatter type="plain" usefile="false"/>
@@ -157,5 +155,12 @@
 
   </target>
 
+  <target name="XLoggerBySystemProps" depends="build, slf4j-simple.jar, cleanOutputDir">
+	    <junit printsummary="yes" fork="yes" haltonfailure="yes">
+	      <classpath refid="simple.classpath"/>
+	      <formatter type="plain" usefile="false"/>
+	      <test name="org.slf4j.XLoggerBySystemPropsTest" />
+	    </junit>
+	  </target>
 
 </project>

Added: slf4j/trunk/tests/src/java/org/slf4j/SimpleLoggerFAFactory.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/tests/src/java/org/slf4j/SimpleLoggerFAFactory.java	Fri May 20 17:48:07 2005
@@ -0,0 +1,10 @@
+package org.slf4j;
+
+import org.slf4j.impl.SimpleLoggerFA;
+
+public class SimpleLoggerFAFactory {
+
+	public static LoggerFactoryAdapter getInstance() {
+		return new SimpleLoggerFA();
+	}
+}

Added: slf4j/trunk/tests/src/java/org/slf4j/XLoggerBySystemPropsTest.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/tests/src/java/org/slf4j/XLoggerBySystemPropsTest.java	Fri May 20 17:48:07 2005
@@ -0,0 +1,29 @@
+/*
+ * Created on May 20, 2005
+ *
+ * TODO To change the template for this generated file go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+package org.slf4j;
+
+import org.slf4j.impl.XLogger;
+
+import junit.framework.TestCase;
+
+/**
+ * @author ceki
+ *
+ * TODO To change the template for this generated type comment go to
+ * Window - Preferences - Java - Code Style - Code Templates
+ */
+public class XLoggerBySystemPropsTest extends TestCase {
+
+	public void testBasic() {
+		System.setProperty(Constants.LOGGER_FA_FACTORY, "org.slf4j.XLoggerFAFactory");
+		
+		Logger logger = LoggerFactory.getLogger("foo");
+		if(! (logger instanceof XLogger)) {
+			fail("returned logger of type "+logger.getClass().getName()+" is not of type SimpleLogger");
+		}
+	}
+}

Added: slf4j/trunk/tests/src/java/org/slf4j/XLoggerFAFactory.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/tests/src/java/org/slf4j/XLoggerFAFactory.java	Fri May 20 17:48:07 2005
@@ -0,0 +1,11 @@
+package org.slf4j;
+
+import org.slf4j.impl.XLoggerFA;
+
+
+public class XLoggerFAFactory {
+
+	public static LoggerFactoryAdapter getInstance() {
+		return new XLoggerFA();
+	}
+}

Added: slf4j/trunk/tests/src/java/org/slf4j/impl/XLogger.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/tests/src/java/org/slf4j/impl/XLogger.java	Fri May 20 17:48:07 2005
@@ -0,0 +1,110 @@
+
+package org.slf4j.impl;
+
+import org.slf4j.Logger;
+
+
+/**
+ * A NOP Logger implementation.
+ */
+public class XLogger implements Logger {
+
+  /**
+   * The unique instance of NOPLogger.
+   */
+  public final static XLogger X_LOGGER = new XLogger();
+  
+  
+  private XLogger() { }
+  
+
+  public boolean isDebugEnabled() {  return false; }
+
+  public void debug(Object msg) {
+  }
+
+  public void debug(Object parameterizedMsg, Object param1) {  }
+
+  public void debug(String parameterizedMsg, Object param1, Object param2) {  }
+
+  public void debug(Object msg, Throwable t) {  }
+
+  public boolean isInfoEnabled() {  return false;
+  }
+
+  /* A NOP implementation.
+   * @see org.slf4j.Logger#info(java.lang.Object)
+   */
+  public void info(Object msg) {
+    // NOP
+  }
+
+  /* A NOP implementation.
+   * @see org.slf4j.Logger#info(java.lang.Object, java.lang.Object)
+   */
+  public void info(Object parameterizedMsg, Object param1) {
+    // NOP
+  }
+
+  /* A NOP implementation.
+   * @see org.slf4j.Logger#info(java.lang.Object, java.lang.Object, java.lang.Object)
+   */
+  public void info(String parameterizedMsg, Object param1, Object param2) {
+    // NOP
+  }
+
+  /* A NOP implementation.
+   * @see org.slf4j.Logger#info(java.lang.Object, java.lang.Throwable)
+   */
+  public void info(Object msg, Throwable t) {
+    // NOP
+  }
+
+  /* Always returns false.
+   * @see org.slf4j.Logger#isWarnEnabled()
+   */
+  public boolean isWarnEnabled() {
+    return false;
+  }
+
+  /* A NOP implementation.
+   * @see org.slf4j.Logger#warn(java.lang.Object)
+   */
+  public void warn(Object msg) {
+    // NOP
+  }
+
+  /* A NOP implementation.
+   * @see org.slf4j.Logger#warn(java.lang.Object, java.lang.Object)
+   */
+  public void warn(Object parameterizedMsg, Object param1) {
+    // NOP
+  }
+
+  /* A NOP implementation.
+   * @see org.slf4j.Logger#warn(java.lang.Object, java.lang.Object, java.lang.Object)
+   */
+  public void warn(String parameterizedMsg, Object param1, Object param2) {
+    // NOP
+  }
+
+  public void warn(Object msg, Throwable t) {
+  }
+
+  public boolean isErrorEnabled() {
+    return false;
+  }
+
+  public void error(Object msg) {
+  }
+
+  public void error(Object parameterizedMsg, Object param1) {
+  }
+
+  public void error(String parameterizedMsg, Object param1, Object param2) {
+  }
+
+  public void error(Object msg, Throwable t) {
+  }
+}
+

Added: slf4j/trunk/tests/src/java/org/slf4j/impl/XLoggerFA.java
==============================================================================
--- (empty file)
+++ slf4j/trunk/tests/src/java/org/slf4j/impl/XLoggerFA.java	Fri May 20 17:48:07 2005
@@ -0,0 +1,26 @@
+
+package org.slf4j.impl;
+
+import org.slf4j.LoggerFactoryAdapter;
+import org.slf4j.Logger;
+
+
+/**
+ * NOPLoggerFA is am implementation of {@link LoggerFactoryAdapter}
+ * which always returns the unique instance of NOPLogger.
+ * 
+ * @author Ceki Gulcu
+ */
+public class XLoggerFA implements LoggerFactoryAdapter {
+  
+  public XLoggerFA() {
+    // nothing to do
+  }
+  
+  public Logger getLogger(String name) {
+    return XLogger.X_LOGGER;
+  }
+  public Logger getLogger(String domainName, String subDomainName) {
+    return XLogger.X_LOGGER;  
+  }  
+}



More information about the slf4j-dev mailing list