com.afunms.common.util.logging.LogFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.afunms.common.util.logging.LogFactory.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.afunms.common.util.logging;

import java.util.Properties;
import java.util.logging.LogManager;

/**
 * Modified LogFactory: removed all discovery, hardcode a specific implementation
 * If you like a different logging implementation - use either the discovery-based
 * commons-logging, or better - another implementation hardcoded to your favourite
 * logging impl.
 * 
 * Why ? Each application and deployment can choose a logging implementation - 
 * that involves configuration, installing the logger jar and optional plugins, etc.
 * As part of this process - they can as well install the commons-logging implementation
 * that corresponds to their logger of choice. This completely avoids any discovery
 * problem, while still allowing the user to switch. 
 * 
 * Note that this implementation is not just a wrapper arround JDK logging ( like
 * the original commons-logging impl ). It adds 2 features - a simpler configuration
 * ( which is in fact a subset of log4j.properties ) and a formatter that is 
 * less ugly.   
 * 
 * The removal of 'abstract' preserves binary backward compatibility. It is possible
 * to preserve the abstract - and introduce another ( hardcoded ) factory - but I 
 * see no benefit. 
 * 
 * Since this class is not intended to be extended - and provides
 * no plugin for other LogFactory implementation - all protected methods are removed.
 * This can be changed - but again, there is little value in keeping dead code.
 * Just take a quick look at the removed code ( and it's complexity)  
 * 
 * --------------
 * 
 * Original comment:
 * <p>Factory for creating {@link Log} instances, with discovery and
 * configuration features similar to that employed by standard Java APIs
 * such as JAXP.</p>
 * 
 * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation is heavily
 * based on the SAXParserFactory and DocumentBuilderFactory implementations
 * (corresponding to the JAXP pluggability APIs) found in Apache Xerces.</p>
 * 
 *
 * @author 
 * @version $Revision: 1.0 $ $Date: 2011-03-01 11:39:25 +0100 (Tue, 01 Mar 2011) $
 */
public /* abstract */ class LogFactory {

    // ----------------------------------------------------- Manifest Constants

    /**
     *  LogFactory 
     */
    public static final String FACTORY_PROPERTY = "org.apache.commons.logging.LogFactory";

    /**
     *  LogFactory 
     */
    public static final String FACTORY_DEFAULT = "org.apache.commons.logging.impl.LogFactoryImpl";

    /**
     * 
     */
    public static final String FACTORY_PROPERTIES = "commons-logging.properties";

    /**
     * <p>Setting this system property value allows the <code>Hashtable</code> used to store
     * classloaders to be substituted by an alternative implementation.
     * </p>
     * <p>
     * <strong>Note:</strong> <code>LogFactory</code> will print:
     * <code><pre>
     * [ERROR] LogFactory: Load of custom hashtable failed</em>
     * </code></pre>
     * to system error and then continue using a standard Hashtable.
     * </p>
     * <p>
     * <strong>Usage:</strong> Set this property when Java is invoked
     * and <code>LogFactory</code> will attempt to load a new instance 
     * of the given implementation class.
     * For example, running the following ant scriplet:
     * <code><pre>
     *  &lt;java classname="${test.runner}" fork="yes" failonerror="${test.failonerror}"&gt;
     *     ...
     *     &lt;sysproperty 
     *        key="org.apache.commons.logging.LogFactory.HashtableImpl"
     *        value="org.apache.commons.logging.AltHashtable"/&gt;
     *  &lt;/java&gt;
     * </pre></code>
     * will mean that <code>LogFactory</code> will load an instance of
     * <code>org.apache.commons.logging.AltHashtable</code>.
     * </p>
     * <p>
     * A typical use case is to allow a custom
     * Hashtable implementation using weak references to be substituted.
     * This will allow classloaders to be garbage collected without
     * the need to release them (on 1.3+ JVMs only, of course ;)
     * </p>
     */
    public static final String HASHTABLE_IMPLEMENTATION_PROPERTY = "org.apache.commons.logging.LogFactory.HashtableImpl";

    private static LogFactory singleton = new LogFactory();

    Properties logConfig;

    // ----------------------------------------------------------- Constructors

    /**
     *  (Singleton)
     */
    private LogFactory() {
        logConfig = new Properties();
    }

    // hook for syserr logger - class level
    void setLogConfig(Properties p) {
        this.logConfig = p;
    }
    // --------------------------------------------------------- Public Methods

    // 

    /**
     * <p>() <code>Log</code>
     * </p>
     *
     * <p><strong></strong> -  <code>LogFactory</code>
     * ,  <code>Log</code> , 
     *  <code>name</code> </p>
     *
     * @param name  <code>Log</code> (
     * ) <code>Log</code> 
     *
     * @exception LogConfigurationException  <code>Log</code>
     * 
     */
    public Log getInstance(String name) throws LogConfigurationException {
        return DirectJDKLog.getInstance(name);
    }

    /**
     * 
     *  {@link Log} 
     *  <code>ClassLoader</code> 
     *  <code>servlet</code> 
     */
    public void release() {
        DirectJDKLog.release();
    }

    /**
     *  <code>name</code> 
     *  <code>null</code> .
     *
     * @param name Name of the attribute to return
     */
    public Object getAttribute(String name) {
        return logConfig.get(name);
    }

    /**
     *  
     * 
     */
    public String[] getAttributeNames() {
        String result[] = new String[logConfig.size()];
        return logConfig.keySet().toArray(result);
    }

    /**
     *  <code>name</code> 
     * 
     *
     * @param name 
     */
    public void removeAttribute(String name) {
        logConfig.remove(name);
    }

    /**
     * 
     *    <code>null</code>
     *  <code>removeAttribute(name)</code> 
     *  
     * @param name 
     * @param value  <code>null</code>
     * 
     */
    public void setAttribute(String name, Object value) {
        logConfig.put(name, value);
    }

    /**
     *  <code>Class</code> 
     *  <code>getInstance(String)</code>  <code>Log</code>
     * 
     *
     * @param clazz  <code>Log</code> 
     * <code>Class</code>
     *
     * @exception LogConfigurationException 
     *  <code>Log</code> 
     */
    public Log getInstance(Class clazz) throws LogConfigurationException {
        return getInstance(clazz.getName());
    }

    // ------------------------------------------------------- 

    // --------------------------------------------------------- 

    /**
     * <p> <code>LogFactory</code> 
     * </p>
     * <ul>
     * <li> <code>org.apache.commons.logging.LogFactory</code> </li>
     * <li>JDK 1.3 </li>
     * <li> <code>commons-logging.properties</code> 
     *       <code>java.util.Properties</code> 
     *     
     * <li>
     *     (<code>org.apache.commons.logging.impl.LogFactoryImpl</code>).</li>
     * </ul>
     *
     * <p><em></em> -  <code>LogFactory</code> 
     *  <code>LogFactory</code> 
     * </p>
     *
     * @exception LogConfigurationException 
     */
    public static LogFactory getFactory() throws LogConfigurationException {
        return singleton;
    }

    /**
     *  <code>Log</code> 
     * 
     *
     * @param clazz  <code>Class</code>
     *
     * @exception LogConfigurationException  
     * <code>Log</code> 
     */
    public static Log getLog(Class clazz) throws LogConfigurationException {
        return (getFactory().getInstance(clazz));
    }

    /**
     *  <code>Log</code> 
     * 
     *
     * @param name  <code>Log</code> (
     * ) <code>Log</code> 
     *
     * @exception LogConfigurationException  
     * <code>Log</code> 
     */
    public static Log getLog(String name) throws LogConfigurationException {
        return (getFactory().getInstance(name));
    }

    /**
     *  {@link LogFactory} 
     *  <code>release()</code> 
     *
     * @param classLoader  <code>LogFactory</code>  {@link ClassLoader}
     */
    public static void release(@SuppressWarnings("unused") ClassLoader classLoader) {
        // JULI's log manager looks at the current classLoader
        LogManager.getLogManager().reset();
    }

    /**
     *  {@link LogFactory} 
     *  <code>release()</code> 
     *  <code>ClassLoader</code> 
     *  <code>servlet</code> 
     */
    public static void releaseAll() {
        singleton.release();
    }

    /**
     * 
     * 
     * <p>"classname@hashcode"  <code>Object.toString()</code>
     *  <code>toString</code> </p>
     * 
     * @param o  <code>null<code>.
     * @return string  <code>classname@hashcode</code> , 
     *  <code>null<code>.
     */
    public static String objectId(Object o) {
        if (o == null) {
            return "null";
        } else {
            return o.getClass().getName() + "@" + System.identityHashCode(o);
        }
    }
}