FileDocCategorySizeDatePackage
LogSource.javaAPI DocAndroid 1.5 API8630Wed May 06 22:41:10 BST 2009org.apache.commons.logging

LogSource

public class LogSource extends Object

Factory for creating {@link Log} instances. Applications should call the makeNewLogInstance() method to instantiate new instances of the configured {@link Log} implementation class.

By default, calling getInstance() will use the following algorithm:

  • If Log4J is available, return an instance of org.apache.commons.logging.impl.Log4JLogger.
  • If JDK 1.4 or later is available, return an instance of org.apache.commons.logging.impl.Jdk14Logger.
  • Otherwise, return an instance of org.apache.commons.logging.impl.NoOpLog.

You can change the default behavior in one of two ways:

  • On the startup command line, set the system property org.apache.commons.logging.log to the name of the org.apache.commons.logging.Log implementation class you want to use.
  • At runtime, call LogSource.setLogImplementation().
deprecated
Use {@link LogFactory} instead - The default factory implementation performs exactly the same algorithm as this class did
author
Rod Waldhoff
version
$Id: LogSource.java 155426 2005-02-26 13:10:49Z dirkv $

Fields Summary
protected static Hashtable
logs
protected static boolean
log4jIsAvailable
Is log4j available (in the current classpath)
protected static boolean
jdk14IsAvailable
Is JDK 1.4 logging available
protected static Constructor
logImplctor
Constructor for current log class
Constructors Summary
private LogSource()
Don't allow others to create instances



    // ----------------------------------------------------- Class Initializers

     

        // Is Log4J Available?
        try {
            if (null != Class.forName("org.apache.log4j.Logger")) {
                log4jIsAvailable = true;
            } else {
                log4jIsAvailable = false;
            }
        } catch (Throwable t) {
            log4jIsAvailable = false;
        }

        // Is JDK 1.4 Logging Available?
        try {
            if ((null != Class.forName("java.util.logging.Logger")) &&
                (null != Class.forName("org.apache.commons.logging.impl.Jdk14Logger"))) {
                jdk14IsAvailable = true;
            } else {
                jdk14IsAvailable = false;
            }
        } catch (Throwable t) {
            jdk14IsAvailable = false;
        }

        // Set the default Log implementation
        String name = null;
        try {
            name = System.getProperty("org.apache.commons.logging.log");
            if (name == null) {
                name = System.getProperty("org.apache.commons.logging.Log");
            }
        } catch (Throwable t) {
        }
        if (name != null) {
            try {
                setLogImplementation(name);
            } catch (Throwable t) {
                try {
                    setLogImplementation
                            ("org.apache.commons.logging.impl.NoOpLog");
                } catch (Throwable u) {
                    ;
                }
            }
        } else {
            try {
                if (log4jIsAvailable) {
                    setLogImplementation
                            ("org.apache.commons.logging.impl.Log4JLogger");
                } else if (jdk14IsAvailable) {
                    setLogImplementation
                            ("org.apache.commons.logging.impl.Jdk14Logger");
                } else {
                    setLogImplementation
                            ("org.apache.commons.logging.impl.NoOpLog");
                }
            } catch (Throwable t) {
                try {
                    setLogImplementation
                            ("org.apache.commons.logging.impl.NoOpLog");
                } catch (Throwable u) {
                    ;
                }
            }
        }

    
    
Methods Summary
public static org.apache.commons.logging.LoggetInstance(java.lang.String name)
Get a Log instance by class name

        Log log = (Log) (logs.get(name));
        if (null == log) {
            log = makeNewLogInstance(name);
            logs.put(name, log);
        }
        return log;
    
public static org.apache.commons.logging.LoggetInstance(java.lang.Class clazz)
Get a Log instance by class

        return getInstance(clazz.getName());
    
public static java.lang.String[]getLogNames()
Returns a {@link String} array containing the names of all logs known to me.

        return (String[]) (logs.keySet().toArray(new String[logs.size()]));
    
public static org.apache.commons.logging.LogmakeNewLogInstance(java.lang.String name)
Create a new {@link Log} implementation, based on the given name.

The specific {@link Log} implementation returned is determined by the value of the org.apache.commons.logging.log property. The value of org.apache.commons.logging.log may be set to the fully specified name of a class that implements the {@link Log} interface. This class must also have a public constructor that takes a single {@link String} argument (containing the name of the {@link Log} to be constructed.

When org.apache.commons.logging.log is not set, or when no corresponding class can be found, this method will return a Log4JLogger if the log4j Logger class is available in the {@link LogSource}'s classpath, or a Jdk14Logger if we are on a JDK 1.4 or later system, or NoOpLog if neither of the above conditions is true.

param
name the log name (or category)


        Log log = null;
        try {
            Object[] args = new Object[1];
            args[0] = name;
            log = (Log) (logImplctor.newInstance(args));
        } catch (Throwable t) {
            log = null;
        }
        if (null == log) {
            log = new NoOpLog(name);
        }
        return log;

    
public static voidsetLogImplementation(java.lang.String classname)
Set the log implementation/log implementation factory by the name of the class. The given class must implement {@link Log}, and provide a constructor that takes a single {@link String} argument (containing the name of the log).

        try {
            Class logclass = Class.forName(classname);
            Class[] argtypes = new Class[1];
            argtypes[0] = "".getClass();
            logImplctor = logclass.getConstructor(argtypes);
        } catch (Throwable t) {
            logImplctor = null;
        }
    
public static voidsetLogImplementation(java.lang.Class logclass)
Set the log implementation/log implementation factory by class. The given class must implement {@link Log}, and provide a constructor that takes a single {@link String} argument (containing the name of the log).

        Class[] argtypes = new Class[1];
        argtypes[0] = "".getClass();
        logImplctor = logclass.getConstructor(argtypes);