diff --git a/build.properties.sample b/build.properties.sample new file mode 100644 index 00000000..23fbcecb --- /dev/null +++ b/build.properties.sample @@ -0,0 +1,3 @@ + +slf4j-site=../site +javadoc.dest=${slf4j-site}/docs/api \ No newline at end of file diff --git a/src/filtered-java/org/slf4j/Constants.java b/src/filtered-java/org/slf4j/Constants.java new file mode 100644 index 00000000..0d250df2 --- /dev/null +++ b/src/filtered-java/org/slf4j/Constants.java @@ -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"; +} diff --git a/src/filtered-java/org/slf4j/LoggerFactory.java b/src/filtered-java/org/slf4j/LoggerFactory.java index 4164fc2d..72eecca9 100644 --- a/src/filtered-java/org/slf4j/LoggerFactory.java +++ b/src/filtered-java/org/slf4j/LoggerFactory.java @@ -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,23 +53,77 @@ public class LoggerFactory { 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.@IMPL@LoggerFA"; - System.out.println("SLF4J built for "+adapterClassStr); - try { - adapter = new org.slf4j.impl.@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(); + 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.@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 { + 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) { return adapter.getLogger(name); } diff --git a/src/filtered-java/org/slf4j/impl/package.html b/src/filtered-java/org/slf4j/impl/package.html new file mode 100644 index 00000000..f4311e30 --- /dev/null +++ b/src/filtered-java/org/slf4j/impl/package.html @@ -0,0 +1,17 @@ + + + + + + + + + + + +

Implementations of core logging interfaces defined in the {@link + org.slf4j} package.

+ +
+ + diff --git a/tests/build.xml b/tests/build.xml index 1ba9de70..6a2ca841 100644 --- a/tests/build.xml +++ b/tests/build.xml @@ -144,9 +144,7 @@ - - @@ -157,5 +155,12 @@ + + + + + + + diff --git a/tests/src/java/org/slf4j/SimpleLoggerFAFactory.java b/tests/src/java/org/slf4j/SimpleLoggerFAFactory.java new file mode 100644 index 00000000..27c97cd7 --- /dev/null +++ b/tests/src/java/org/slf4j/SimpleLoggerFAFactory.java @@ -0,0 +1,10 @@ +package org.slf4j; + +import org.slf4j.impl.SimpleLoggerFA; + +public class SimpleLoggerFAFactory { + + public static LoggerFactoryAdapter getInstance() { + return new SimpleLoggerFA(); + } +} diff --git a/tests/src/java/org/slf4j/XLoggerBySystemPropsTest.java b/tests/src/java/org/slf4j/XLoggerBySystemPropsTest.java new file mode 100644 index 00000000..436588ba --- /dev/null +++ b/tests/src/java/org/slf4j/XLoggerBySystemPropsTest.java @@ -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"); + } + } +} diff --git a/tests/src/java/org/slf4j/XLoggerFAFactory.java b/tests/src/java/org/slf4j/XLoggerFAFactory.java new file mode 100644 index 00000000..1fd7f74c --- /dev/null +++ b/tests/src/java/org/slf4j/XLoggerFAFactory.java @@ -0,0 +1,11 @@ +package org.slf4j; + +import org.slf4j.impl.XLoggerFA; + + +public class XLoggerFAFactory { + + public static LoggerFactoryAdapter getInstance() { + return new XLoggerFA(); + } +} diff --git a/tests/src/java/org/slf4j/impl/XLogger.java b/tests/src/java/org/slf4j/impl/XLogger.java new file mode 100644 index 00000000..f835d328 --- /dev/null +++ b/tests/src/java/org/slf4j/impl/XLogger.java @@ -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) { + } +} + diff --git a/tests/src/java/org/slf4j/impl/XLoggerFA.java b/tests/src/java/org/slf4j/impl/XLoggerFA.java new file mode 100644 index 00000000..9c9ba3a2 --- /dev/null +++ b/tests/src/java/org/slf4j/impl/XLoggerFA.java @@ -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; + } +}