public class LogManager extends Object
Logger
. The method getLogger()
is provided as the most convenient way to obtain a named Logger based
on the calling class name. This class also provides method for obtaining named Loggers that use
String.format(String, Object...)
style messages instead of the default type of parameterized messages. These
are obtained through the getFormatterLogger(Class)
family of methods. Other service provider methods are
given through the getContext()
and getFactory()
family of methods; these methods are not normally
useful for typical usage of Log4j.Modifier and Type | Field and Description |
---|---|
static String |
FACTORY_PROPERTY_NAME
Log4j property to set to the fully qualified class name of a custom implementation of
LoggerContextFactory . |
static String |
ROOT_LOGGER_NAME
The name of the root Logger.
|
Modifier | Constructor and Description |
---|---|
protected |
LogManager()
Prevents instantiation
|
Modifier and Type | Method and Description |
---|---|
static boolean |
exists(String name)
Detects if a Logger with the specified name exists.
|
static LoggerContext |
getContext()
Returns the current LoggerContext.
|
static LoggerContext |
getContext(boolean currentContext)
Returns a LoggerContext.
|
static LoggerContext |
getContext(ClassLoader loader,
boolean currentContext)
Returns a LoggerContext.
|
static LoggerContext |
getContext(ClassLoader loader,
boolean currentContext,
Object externalContext)
Returns a LoggerContext.
|
static LoggerContext |
getContext(ClassLoader loader,
boolean currentContext,
Object externalContext,
URI configLocation)
Returns a LoggerContext.
|
static LoggerContext |
getContext(ClassLoader loader,
boolean currentContext,
Object externalContext,
URI configLocation,
String name)
Returns a LoggerContext.
|
static LoggerContext |
getContext(ClassLoader loader,
boolean currentContext,
URI configLocation)
Returns a LoggerContext.
|
protected static LoggerContext |
getContext(String fqcn,
boolean currentContext)
Returns a LoggerContext
|
protected static LoggerContext |
getContext(String fqcn,
ClassLoader loader,
boolean currentContext)
Returns a LoggerContext
|
protected static LoggerContext |
getContext(String fqcn,
ClassLoader loader,
boolean currentContext,
URI configLocation,
String name)
Returns a LoggerContext
|
static LoggerContextFactory |
getFactory()
Returns the current LoggerContextFactory.
|
static Logger |
getFormatterLogger()
Returns a formatter Logger using the fully qualified name of the calling Class as the Logger name.
|
static Logger |
getFormatterLogger(Class<?> clazz)
Returns a formatter Logger using the fully qualified name of the Class as the Logger name.
|
static Logger |
getFormatterLogger(Object value)
Returns a formatter Logger using the fully qualified name of the value's Class as the Logger name.
|
static Logger |
getFormatterLogger(String name)
Returns a formatter Logger with the specified name.
|
static Logger |
getLogger()
Returns a Logger with the name of the calling class.
|
static Logger |
getLogger(Class<?> clazz)
Returns a Logger using the fully qualified name of the Class as the Logger name.
|
static Logger |
getLogger(Class<?> clazz,
MessageFactory messageFactory)
Returns a Logger using the fully qualified name of the Class as the Logger name.
|
static Logger |
getLogger(MessageFactory messageFactory)
Returns a Logger with the name of the calling class.
|
static Logger |
getLogger(Object value)
Returns a Logger using the fully qualified class name of the value as the Logger name.
|
static Logger |
getLogger(Object value,
MessageFactory messageFactory)
Returns a Logger using the fully qualified class name of the value as the Logger name.
|
static Logger |
getLogger(String name)
Returns a Logger with the specified name.
|
static Logger |
getLogger(String name,
MessageFactory messageFactory)
Returns a Logger with the specified name.
|
protected static Logger |
getLogger(String fqcn,
String name)
Returns a Logger with the specified name.
|
static Logger |
getRootLogger()
Returns the root logger.
|
static void |
setFactory(LoggerContextFactory factory)
Sets the current LoggerContextFactory to use.
|
static void |
shutdown()
Shutdown using the LoggerContext appropriate for the caller of this method.
|
static void |
shutdown(boolean currentContext)
Shutdown the logging system if the logging system supports it.
|
static void |
shutdown(boolean currentContext,
boolean allContexts)
Shutdown the logging system if the logging system supports it.
|
static void |
shutdown(LoggerContext context)
Shutdown the logging system if the logging system supports it.
|
public static final String FACTORY_PROPERTY_NAME
LoggerContextFactory
.public static final String ROOT_LOGGER_NAME
protected LogManager()
public static boolean exists(String name)
name
- The Logger name to search for.LoggerContext.hasLogger(String)
public static LoggerContext getContext()
WARNING - The LoggerContext returned by this method may not be the LoggerContext used to create a Logger for the calling class.
public static LoggerContext getContext(boolean currentContext)
currentContext
- if false the LoggerContext appropriate for the caller of this method is returned. For
example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
returned and if the caller is a class in the container's classpath then a different LoggerContext may
be returned. If true then only a single LoggerContext will be returned.public static LoggerContext getContext(ClassLoader loader, boolean currentContext)
loader
- The ClassLoader for the context. If null the context will attempt to determine the appropriate
ClassLoader.currentContext
- if false the LoggerContext appropriate for the caller of this method is returned. For
example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
returned and if the caller is a class in the container's classpath then a different LoggerContext may
be returned. If true then only a single LoggerContext will be returned.public static LoggerContext getContext(ClassLoader loader, boolean currentContext, Object externalContext)
loader
- The ClassLoader for the context. If null the context will attempt to determine the appropriate
ClassLoader.currentContext
- if false the LoggerContext appropriate for the caller of this method is returned. For
example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
returned and if the caller is a class in the container's classpath then a different LoggerContext may
be returned. If true then only a single LoggerContext will be returned.externalContext
- An external context (such as a ServletContext) to be associated with the LoggerContext.public static LoggerContext getContext(ClassLoader loader, boolean currentContext, URI configLocation)
loader
- The ClassLoader for the context. If null the context will attempt to determine the appropriate
ClassLoader.currentContext
- if false the LoggerContext appropriate for the caller of this method is returned. For
example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
returned and if the caller is a class in the container's classpath then a different LoggerContext may
be returned. If true then only a single LoggerContext will be returned.configLocation
- The URI for the configuration to use.public static LoggerContext getContext(ClassLoader loader, boolean currentContext, Object externalContext, URI configLocation)
loader
- The ClassLoader for the context. If null the context will attempt to determine the appropriate
ClassLoader.currentContext
- if false the LoggerContext appropriate for the caller of this method is returned. For
example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
returned and if the caller is a class in the container's classpath then a different LoggerContext may
be returned. If true then only a single LoggerContext will be returned.externalContext
- An external context (such as a ServletContext) to be associated with the LoggerContext.configLocation
- The URI for the configuration to use.public static LoggerContext getContext(ClassLoader loader, boolean currentContext, Object externalContext, URI configLocation, String name)
loader
- The ClassLoader for the context. If null the context will attempt to determine the appropriate
ClassLoader.currentContext
- if false the LoggerContext appropriate for the caller of this method is returned. For
example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
returned and if the caller is a class in the container's classpath then a different LoggerContext may
be returned. If true then only a single LoggerContext will be returned.externalContext
- An external context (such as a ServletContext) to be associated with the LoggerContext.configLocation
- The URI for the configuration to use.name
- The LoggerContext name.protected static LoggerContext getContext(String fqcn, boolean currentContext)
fqcn
- The fully qualified class name of the Class that this method is a member of.currentContext
- if false the LoggerContext appropriate for the caller of this method is returned. For
example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
returned and if the caller is a class in the container's classpath then a different LoggerContext may
be returned. If true then only a single LoggerContext will be returned.protected static LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext)
fqcn
- The fully qualified class name of the Class that this method is a member of.loader
- The ClassLoader for the context. If null the context will attempt to determine the appropriate
ClassLoader.currentContext
- if false the LoggerContext appropriate for the caller of this method is returned. For
example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
returned and if the caller is a class in the container's classpath then a different LoggerContext may
be returned. If true then only a single LoggerContext will be returned.protected static LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext, URI configLocation, String name)
fqcn
- The fully qualified class name of the Class that this method is a member of.loader
- The ClassLoader for the context. If null the context will attempt to determine the appropriate
ClassLoader.currentContext
- if false the LoggerContext appropriate for the caller of this method is returned. For
example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
returned and if the caller is a class in the container's classpath then a different LoggerContext may
be returned. If true then only a single LoggerContext will be returned.configLocation
- The URI for the configuration to use.name
- The LoggerContext name.public static void shutdown()
LogManager.shutdown(false)
.
This call is synchronous and will block until shut down is complete.
This may include flushing pending log events over network connections.public static void shutdown(boolean currentContext)
LogManager.shutdown(LogManager.getContext(currentContext))
.
This call is synchronous and will block until shut down is complete.
This may include flushing pending log events over network connections.currentContext
- if true a default LoggerContext (may not be the LoggerContext used to create a Logger
for the calling class) will be used.
If false the LoggerContext appropriate for the caller of this method is used. For
example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
used and if the caller is a class in the container's classpath then a different LoggerContext may
be used.public static void shutdown(boolean currentContext, boolean allContexts)
LogManager.shutdown(LogManager.getContext(currentContext))
.
This call is synchronous and will block until shut down is complete.
This may include flushing pending log events over network connections.currentContext
- if true a default LoggerContext (may not be the LoggerContext used to create a Logger
for the calling class) will be used.
If false the LoggerContext appropriate for the caller of this method is used. For
example, in a web application if the caller is a class in WEB-INF/lib then one LoggerContext may be
used and if the caller is a class in the container's classpath then a different LoggerContext may
be used.allContexts
- if true all LoggerContexts that can be located will be shutdown.public static void shutdown(LoggerContext context)
context
- the LoggerContext.public static LoggerContextFactory getFactory()
public static void setFactory(LoggerContextFactory factory)
Note that any Logger or LoggerContext objects already created will still be valid, but they will no longer be accessible through LogManager. Thus, it is a bad idea to use this method without a good reason! Generally, this method should be used only during startup before any code starts caching Logger objects.
factory
- the LoggerContextFactory to use.public static Logger getFormatterLogger()
This logger lets you use a Formatter
string in the message to format parameters.
UnsupportedOperationException
- if the calling class cannot be determined.public static Logger getFormatterLogger(Class<?> clazz)
This logger let you use a Formatter
string in the message to format parameters.
Short-hand for getLogger(clazz, StringFormatterMessageFactory.INSTANCE)
clazz
- The Class whose name should be used as the Logger name.StringFormatterMessageFactory
UnsupportedOperationException
- if clazz
is null
and the calling class cannot be
determined.Logger.fatal(Marker, String, Object...)
,
Logger.fatal(String, Object...)
,
Logger.error(Marker, String, Object...)
,
Logger.error(String, Object...)
,
Logger.warn(Marker, String, Object...)
,
Logger.warn(String, Object...)
,
Logger.info(Marker, String, Object...)
,
Logger.info(String, Object...)
,
Logger.debug(Marker, String, Object...)
,
Logger.debug(String, Object...)
,
Logger.trace(Marker, String, Object...)
,
Logger.trace(String, Object...)
,
StringFormatterMessageFactory
public static Logger getFormatterLogger(Object value)
This logger let you use a Formatter
string in the message to format parameters.
Short-hand for getLogger(value, StringFormatterMessageFactory.INSTANCE)
value
- The value's whose class name should be used as the Logger name.StringFormatterMessageFactory
UnsupportedOperationException
- if value
is null
and the calling class cannot be
determined.Logger.fatal(Marker, String, Object...)
,
Logger.fatal(String, Object...)
,
Logger.error(Marker, String, Object...)
,
Logger.error(String, Object...)
,
Logger.warn(Marker, String, Object...)
,
Logger.warn(String, Object...)
,
Logger.info(Marker, String, Object...)
,
Logger.info(String, Object...)
,
Logger.debug(Marker, String, Object...)
,
Logger.debug(String, Object...)
,
Logger.trace(Marker, String, Object...)
,
Logger.trace(String, Object...)
,
StringFormatterMessageFactory
public static Logger getFormatterLogger(String name)
This logger let you use a Formatter
string in the message to format parameters.
Short-hand for getLogger(name, StringFormatterMessageFactory.INSTANCE)
name
- The logger name. If null it will default to the name of the calling class.StringFormatterMessageFactory
UnsupportedOperationException
- if name
is null
and the calling class cannot be determined.Logger.fatal(Marker, String, Object...)
,
Logger.fatal(String, Object...)
,
Logger.error(Marker, String, Object...)
,
Logger.error(String, Object...)
,
Logger.warn(Marker, String, Object...)
,
Logger.warn(String, Object...)
,
Logger.info(Marker, String, Object...)
,
Logger.info(String, Object...)
,
Logger.debug(Marker, String, Object...)
,
Logger.debug(String, Object...)
,
Logger.trace(Marker, String, Object...)
,
Logger.trace(String, Object...)
,
StringFormatterMessageFactory
public static Logger getLogger()
UnsupportedOperationException
- if the calling class cannot be determined.public static Logger getLogger(Class<?> clazz)
clazz
- The Class whose name should be used as the Logger name. If null it will default to the calling
class.UnsupportedOperationException
- if clazz
is null
and the calling class cannot be
determined.public static Logger getLogger(Class<?> clazz, MessageFactory messageFactory)
clazz
- The Class whose name should be used as the Logger name. If null it will default to the calling
class.messageFactory
- The message factory is used only when creating a logger, subsequent use does not change the
logger but will log a warning if mismatched.UnsupportedOperationException
- if clazz
is null
and the calling class cannot be
determined.public static Logger getLogger(MessageFactory messageFactory)
messageFactory
- The message factory is used only when creating a logger, subsequent use does not change the
logger but will log a warning if mismatched.UnsupportedOperationException
- if the calling class cannot be determined.public static Logger getLogger(Object value)
value
- The value whose class name should be used as the Logger name. If null the name of the calling class
will be used as the logger name.UnsupportedOperationException
- if value
is null
and the calling class cannot be
determined.public static Logger getLogger(Object value, MessageFactory messageFactory)
value
- The value whose class name should be used as the Logger name. If null the name of the calling class
will be used as the logger name.messageFactory
- The message factory is used only when creating a logger, subsequent use does not change the
logger but will log a warning if mismatched.UnsupportedOperationException
- if value
is null
and the calling class cannot be
determined.public static Logger getLogger(String name)
name
- The logger name. If null the name of the calling class will be used.UnsupportedOperationException
- if name
is null
and the calling class cannot be determined.public static Logger getLogger(String name, MessageFactory messageFactory)
name
- The logger name. If null the name of the calling class will be used.messageFactory
- The message factory is used only when creating a logger, subsequent use does not change the
logger but will log a warning if mismatched.UnsupportedOperationException
- if name
is null
and the calling class cannot be determined.protected static Logger getLogger(String fqcn, String name)
fqcn
- The fully qualified class name of the class that this method is a member of.name
- The logger name.public static Logger getRootLogger()
ROOT_LOGGER_NAME
.Copyright © 1999-1969 The Apache Software Foundation. All Rights Reserved.
Apache Logging, Apache Log4j, Log4j, Apache, the Apache feather logo, the Apache Logging project logo, and the Apache Log4j logo are trademarks of The Apache Software Foundation.