public class LogFactoryImpl extends LogFactory
LogFactory
that implements the
following algorithm to dynamically select a logging implementation
class to instantiate a wrapper for:
org.apache.commons.logging.Log
to identify the
requested implementation class.org.apache.commons.logging.Log
system property
to identify the requested implementation class.org.apache.commons.logging.impl.Log4JLogger
.org.apache.commons.logging.impl.Jdk14Logger
.org.apache.commons.logging.impl.SimpleLog
.
If the selected Log
implementation class has a
setLogFactory()
method that accepts a LogFactory
parameter, this method will be called on each newly created instance
to identify the associated factory. This makes factory configuration
attributes available to the Log instance, if it so desires.
This factory will remember previously created Log
instances
for the same name, and will return them on repeated requests to the
getInstance()
method.
Modifier and Type | Field and Description |
---|---|
static String |
ALLOW_FLAWED_CONTEXT_PROPERTY
The name (
org.apache.commons.logging.Log.allowFlawedContext )
of the system property which can be set true/false to
determine system behaviour when a bad context-classloader is encountered. |
static String |
ALLOW_FLAWED_DISCOVERY_PROPERTY
The name (
org.apache.commons.logging.Log.allowFlawedDiscovery )
of the system property which can be set true/false to
determine system behaviour when a bad logging adapter class is
encountered during logging discovery. |
static String |
ALLOW_FLAWED_HIERARCHY_PROPERTY
The name (
org.apache.commons.logging.Log.allowFlawedHierarchy )
of the system property which can be set true/false to
determine system behaviour when a logging adapter class is
encountered which has bound to the wrong Log class implementation. |
protected Hashtable |
attributes
Configuration attributes.
|
protected Hashtable |
instances
The
Log instances that have
already been created, keyed by logger name. |
static String |
LOG_PROPERTY
The name (
org.apache.commons.logging.Log ) of the system
property identifying our Log implementation class. |
protected static String |
LOG_PROPERTY_OLD
The deprecated system property used for backwards compatibility with
old versions of JCL.
|
protected Constructor |
logConstructor
The one-argument constructor of the
Log
implementation class that will be used to create new instances. |
protected Class[] |
logConstructorSignature
The signature of the Constructor to be used.
|
protected Method |
logMethod
The one-argument
setLogFactory method of the selected
Log method, if it exists. |
protected Class[] |
logMethodSignature
The signature of the
setLogFactory method to be used. |
DIAGNOSTICS_DEST_PROPERTY, factories, FACTORY_DEFAULT, FACTORY_PROPERTIES, FACTORY_PROPERTY, HASHTABLE_IMPLEMENTATION_PROPERTY, nullClassLoaderFactory, PRIORITY_KEY, SERVICE_ID, TCCL_KEY
Constructor and Description |
---|
LogFactoryImpl()
Public no-arguments constructor required by the lookup mechanism.
|
Modifier and Type | Method and Description |
---|---|
Object |
getAttribute(String name)
Return the configuration attribute with the specified name (if any),
or
null if there is no such attribute. |
String[] |
getAttributeNames()
Return an array containing the names of all currently defined
configuration attributes.
|
protected static ClassLoader |
getClassLoader(Class clazz)
Workaround for bug in Java1.2; in theory this method is not needed.
|
protected static ClassLoader |
getContextClassLoader()
Gets the context classloader.
|
Log |
getInstance(Class clazz)
Convenience method to derive a name from the specified class and
call
getInstance(String) with it. |
Log |
getInstance(String name)
Construct (if necessary) and return a
Log instance,
using the factory's current set of configuration attributes. |
protected String |
getLogClassName()
Deprecated.
Never invoked by this class; subclasses should not assume
it will be.
|
protected Constructor |
getLogConstructor()
Deprecated.
Never invoked by this class; subclasses should not assume
it will be.
|
protected static boolean |
isDiagnosticsEnabled()
Workaround for bug in Java1.2; in theory this method is not needed.
|
protected boolean |
isJdk13LumberjackAvailable()
Deprecated.
Never invoked by this class; subclasses should not assume
it will be.
|
protected boolean |
isJdk14Available()
Deprecated.
Never invoked by this class; subclasses should not assume
it will be.
|
protected boolean |
isLog4JAvailable()
Deprecated.
Never invoked by this class; subclasses should not assume
it will be.
|
protected void |
logDiagnostic(String msg)
Output a diagnostic message to a user-specified destination (if the
user has enabled diagnostic logging).
|
protected Log |
newInstance(String name)
Create and return a new
Log
instance for the specified name. |
void |
release()
Release any internal references to previously created
Log
instances returned by this factory. |
void |
removeAttribute(String name)
Remove any configuration attribute associated with the specified name.
|
void |
setAttribute(String name,
Object value)
Set the configuration attribute with the specified name.
|
createFactory, directGetContextClassLoader, getFactory, getLog, getLog, handleThrowable, logRawDiagnostic, newFactory, newFactory, objectId, release, releaseAll
public static final String LOG_PROPERTY
org.apache.commons.logging.Log
) of the system
property identifying our Log
implementation class.protected static final String LOG_PROPERTY_OLD
public static final String ALLOW_FLAWED_CONTEXT_PROPERTY
org.apache.commons.logging.Log.allowFlawedContext
)
of the system property which can be set true/false to
determine system behaviour when a bad context-classloader is encountered.
When set to false, a LogConfigurationException is thrown if
LogFactoryImpl is loaded via a child classloader of the TCCL (this
should never happen in sane systems).
Default behaviour: true (tolerates bad context classloaders)
See also method setAttribute.public static final String ALLOW_FLAWED_DISCOVERY_PROPERTY
org.apache.commons.logging.Log.allowFlawedDiscovery
)
of the system property which can be set true/false to
determine system behaviour when a bad logging adapter class is
encountered during logging discovery. When set to false, an
exception will be thrown and the app will fail to start. When set
to true, discovery will continue (though the user might end up
with a different logging implementation than they expected).
Default behaviour: true (tolerates bad logging adapters) See also method setAttribute.
public static final String ALLOW_FLAWED_HIERARCHY_PROPERTY
org.apache.commons.logging.Log.allowFlawedHierarchy
)
of the system property which can be set true/false to
determine system behaviour when a logging adapter class is
encountered which has bound to the wrong Log class implementation.
When set to false, an exception will be thrown and the app will fail
to start. When set to true, discovery will continue (though the user
might end up with a different logging implementation than they expected).
Default behaviour: true (tolerates bad Log class hierarchy) See also method setAttribute.
protected Hashtable attributes
protected Hashtable instances
Log
instances that have
already been created, keyed by logger name.protected Constructor logConstructor
Log
implementation class that will be used to create new instances.
This value is initialized by getLogConstructor()
,
and then returned repeatedly.protected Class[] logConstructorSignature
protected Method logMethod
setLogFactory
method of the selected
Log
method, if it exists.protected Class[] logMethodSignature
setLogFactory
method to be used.public LogFactoryImpl()
public Object getAttribute(String name)
null
if there is no such attribute.getAttribute
in class LogFactory
name
- Name of the attribute to returnpublic String[] getAttributeNames()
getAttributeNames
in class LogFactory
public Log getInstance(Class clazz) throws LogConfigurationException
getInstance(String)
with it.getInstance
in class LogFactory
clazz
- Class for which a suitable Log name will be derivedLogConfigurationException
- if a suitable Log
instance cannot be returnedpublic Log getInstance(String name) throws LogConfigurationException
Construct (if necessary) and return a Log
instance,
using the factory's current set of configuration attributes.
NOTE - Depending upon the implementation of
the LogFactory
you are using, the Log
instance you are returned may or may not be local to the current
application, and may or may not be returned again on a subsequent
call with the same name argument.
getInstance
in class LogFactory
name
- Logical name of the Log
instance to be
returned (the meaning of this name is only known to the underlying
logging implementation that is being wrapped)LogConfigurationException
- if a suitable Log
instance cannot be returnedpublic void release()
Log
instances returned by this factory. This is useful in 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.release
in class LogFactory
public void removeAttribute(String name)
removeAttribute
in class LogFactory
name
- Name of the attribute to removepublic void setAttribute(String name, Object value)
null
value is equivalent to calling
removeAttribute(name)
.
This method can be used to set logging configuration programmatically rather than via system properties. It can also be used in code running within a container (such as a webapp) to configure behaviour on a per-component level instead of globally as system properties would do. To use this method instead of a system property, call
LogFactory.getFactory().setAttribute(...)This must be done before the first Log object is created; configuration changes after that point will be ignored.
This method is also called automatically if LogFactory detects a commons-logging.properties file; every entry in that file is set automatically as an attribute here.
setAttribute
in class LogFactory
name
- Name of the attribute to setvalue
- Value of the attribute to set, or null
to remove any setting for this attributeprotected static ClassLoader getContextClassLoader() throws LogConfigurationException
LogConfigurationException
protected static boolean isDiagnosticsEnabled()
protected static ClassLoader getClassLoader(Class clazz)
protected void logDiagnostic(String msg)
msg
- diagnostic messageprotected String getLogClassName()
Log
implementation we will be using.protected Constructor getLogConstructor() throws LogConfigurationException
Return the Constructor
that can be called to instantiate
new Log
instances.
IMPLEMENTATION NOTE - Race conditions caused by
calling this method from more than one thread are ignored, because
the same Constructor
instance will ultimately be derived
in all circumstances.
LogConfigurationException
- if a suitable constructor
cannot be returnedprotected boolean isJdk13LumberjackAvailable()
protected boolean isJdk14Available()
true
if JDK 1.4 or later logging
is available. Also checks that the Throwable
class
supports getStackTrace()
, which is required by
Jdk14Logger.protected boolean isLog4JAvailable()
protected Log newInstance(String name) throws LogConfigurationException
Log
instance for the specified name.name
- Name of the new loggerLogConfigurationException
- if a new instance cannot
be createdCopyright © 2001–2014 The Apache Software Foundation. All rights reserved.