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

This commit is contained in:
Ceki Gulcu 2005-05-20 15:48:07 +00:00
parent 118d91ef27
commit 7b4e60bdd7
10 changed files with 295 additions and 18 deletions

3
build.properties.sample Normal file
View File

@ -0,0 +1,3 @@
slf4j-site=../site
javadoc.dest=${slf4j-site}/docs/api

View File

@ -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";
}

View File

@ -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);
}

View File

@ -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>

View File

@ -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>

View File

@ -0,0 +1,10 @@
package org.slf4j;
import org.slf4j.impl.SimpleLoggerFA;
public class SimpleLoggerFAFactory {
public static LoggerFactoryAdapter getInstance() {
return new SimpleLoggerFA();
}
}

View File

@ -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");
}
}
}

View File

@ -0,0 +1,11 @@
package org.slf4j;
import org.slf4j.impl.XLoggerFA;
public class XLoggerFAFactory {
public static LoggerFactoryAdapter getInstance() {
return new XLoggerFA();
}
}

View File

@ -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) {
}
}

View File

@ -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;
}
}