Added a log4j specific factory. It's cleaner and goes directly
to the logger impl, without introspection. The factory can be extended to take advantage of other log4j features ( it calls getInstance(Class) direclty for example ) to integrate better. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/logging/trunk@138865 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
202
src/java/org/apache/commons/logging/impl/Log4jFactory.java
Normal file
202
src/java/org/apache/commons/logging/impl/Log4jFactory.java
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
/*
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* The Apache Software License, Version 1.1
|
||||||
|
*
|
||||||
|
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
|
||||||
|
* reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
*
|
||||||
|
* 3. The end-user documentation included with the redistribution, if
|
||||||
|
* any, must include the following acknowlegement:
|
||||||
|
* "This product includes software developed by the
|
||||||
|
* Apache Software Foundation (http://www.apache.org/)."
|
||||||
|
* Alternately, this acknowlegement may appear in the software itself,
|
||||||
|
* if and wherever such third-party acknowlegements normally appear.
|
||||||
|
*
|
||||||
|
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||||
|
* Foundation" must not be used to endorse or promote products derived
|
||||||
|
* from this software without prior written permission. For written
|
||||||
|
* permission, please contact apache@apache.org.
|
||||||
|
*
|
||||||
|
* 5. Products derived from this software may not be called "Apache"
|
||||||
|
* nor may "Apache" appear in their names without prior written
|
||||||
|
* permission of the Apache Group.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||||
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||||
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||||
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||||
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
* ====================================================================
|
||||||
|
*
|
||||||
|
* This software consists of voluntary contributions made by many
|
||||||
|
* individuals on behalf of the Apache Software Foundation. For more
|
||||||
|
* information on the Apache Software Foundation, please see
|
||||||
|
* <http://www.apache.org/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.apache.commons.logging.impl;
|
||||||
|
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Hashtable;
|
||||||
|
import java.util.Vector;
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogConfigurationException;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.apache.commons.logging.LogSource;
|
||||||
|
|
||||||
|
import org.apache.log4j.Category;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Concrete subclass of {@link LogFactory} specific to log4j.
|
||||||
|
*
|
||||||
|
* @author Costin Manolache
|
||||||
|
*/
|
||||||
|
public final class Log4jFactory extends LogFactory {
|
||||||
|
|
||||||
|
public Log4jFactory() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The configuration attributes for this {@link LogFactory}.
|
||||||
|
*/
|
||||||
|
private Hashtable attributes = new Hashtable();
|
||||||
|
|
||||||
|
// previously returned instances, to avoid creation of proxies
|
||||||
|
private Hashtable instances = new Hashtable();
|
||||||
|
|
||||||
|
// --------------------------------------------------------- Public Methods
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the configuration attribute with the specified name (if any),
|
||||||
|
* or <code>null</code> if there is no such attribute.
|
||||||
|
*
|
||||||
|
* @param name Name of the attribute to return
|
||||||
|
*/
|
||||||
|
public Object getAttribute(String name) {
|
||||||
|
return (attributes.get(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return an array containing the names of all currently defined
|
||||||
|
* configuration attributes. If there are no such attributes, a zero
|
||||||
|
* length array is returned.
|
||||||
|
*/
|
||||||
|
public String[] getAttributeNames() {
|
||||||
|
Vector names = new Vector();
|
||||||
|
Enumeration keys = attributes.keys();
|
||||||
|
while (keys.hasMoreElements()) {
|
||||||
|
names.addElement((String) keys.nextElement());
|
||||||
|
}
|
||||||
|
String results[] = new String[names.size()];
|
||||||
|
for (int i = 0; i < results.length; i++) {
|
||||||
|
results[i] = (String) names.elementAt(i);
|
||||||
|
}
|
||||||
|
return (results);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convenience method to derive a name from the specified class and
|
||||||
|
* call <code>getInstance(String)</code> with it.
|
||||||
|
*
|
||||||
|
* @param clazz Class for which a suitable Log name will be derived
|
||||||
|
*
|
||||||
|
* @exception LogConfigurationException if a suitable <code>Log</code>
|
||||||
|
* instance cannot be returned
|
||||||
|
*/
|
||||||
|
public Log getInstance(Class clazz)
|
||||||
|
throws LogConfigurationException
|
||||||
|
{
|
||||||
|
Log instance = (Log) instances.get(clazz);
|
||||||
|
if( instance != null )
|
||||||
|
return instance;
|
||||||
|
|
||||||
|
instance=new Log4JCategoryLog( Category.getInstance( clazz ));
|
||||||
|
instances.put( clazz, instance );
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Log getInstance(String name)
|
||||||
|
throws LogConfigurationException
|
||||||
|
{
|
||||||
|
Log instance = (Log) instances.get(name);
|
||||||
|
if( instance != null )
|
||||||
|
return instance;
|
||||||
|
|
||||||
|
instance=new Log4JCategoryLog( Category.getInstance( name ));
|
||||||
|
instances.put( name, instance );
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Release any internal references to previously created {@link Log}
|
||||||
|
* instances returned by this factory. This is useful environments
|
||||||
|
* like servlet containers, which implement application reloading by
|
||||||
|
* throwing away a ClassLoader. Dangling references to objects in that
|
||||||
|
* class loader would prevent garbage collection.
|
||||||
|
*/
|
||||||
|
public void release() {
|
||||||
|
|
||||||
|
instances.clear();
|
||||||
|
|
||||||
|
// what's the log4j mechanism to cleanup ???
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove any configuration attribute associated with the specified name.
|
||||||
|
* If there is no such attribute, no action is taken.
|
||||||
|
*
|
||||||
|
* @param name Name of the attribute to remove
|
||||||
|
*/
|
||||||
|
public void removeAttribute(String name) {
|
||||||
|
attributes.remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the configuration attribute with the specified name. Calling
|
||||||
|
* this with a <code>null</code> value is equivalent to calling
|
||||||
|
* <code>removeAttribute(name)</code>.
|
||||||
|
*
|
||||||
|
* @param name Name of the attribute to set
|
||||||
|
* @param value Value of the attribute to set, or <code>null</code>
|
||||||
|
* to remove any setting for this attribute
|
||||||
|
*/
|
||||||
|
public void setAttribute(String name, Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
attributes.remove(name);
|
||||||
|
} else {
|
||||||
|
attributes.put(name, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user