Class LogManager
- java.lang.Object
-
- org.apache.logging.log4j.LogManager
-
public class LogManager extends java.lang.ObjectThe anchor point for the Log4j logging system. The most common usage of this class is to obtain a namedLogger. The methodgetLogger()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 useString.format(String, Object...)style messages instead of the default type of parameterized messages. These are obtained through thegetFormatterLogger(Class)family of methods. Other service provider methods are given through thegetContext()andgetFactory()family of methods; these methods are not normally useful for typical usage of Log4j.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringFACTORY_PROPERTY_NAMELog4j property to set to the fully qualified class name of a custom implementation ofLoggerContextFactory.static java.lang.StringROOT_LOGGER_NAMEThe name of the root Logger is "".
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanexists(java.lang.String name)Detects if a Logger with the specified name exists.static LoggerContextgetContext()Returns the current LoggerContext.static LoggerContextgetContext(boolean currentContext)Returns a LoggerContext.static LoggerContextgetContext(java.lang.ClassLoader loader, boolean currentContext)Returns a LoggerContext.static LoggerContextgetContext(java.lang.ClassLoader loader, boolean currentContext, java.lang.Object externalContext)Returns a LoggerContext.static LoggerContextgetContext(java.lang.ClassLoader loader, boolean currentContext, java.lang.Object externalContext, java.net.URI configLocation)Returns a LoggerContext.static LoggerContextgetContext(java.lang.ClassLoader loader, boolean currentContext, java.lang.Object externalContext, java.net.URI configLocation, java.lang.String name)Returns a LoggerContext.static LoggerContextgetContext(java.lang.ClassLoader loader, boolean currentContext, java.net.URI configLocation)Returns a LoggerContext.static LoggerContextFactorygetFactory()Returns the current LoggerContextFactory.static LoggergetFormatterLogger()Returns a formatter Logger using the fully qualified name of the calling Class as the Logger name.static LoggergetFormatterLogger(java.lang.Class<?> clazz)Returns a formatter Logger using the fully qualified name of the Class as the Logger name.static LoggergetFormatterLogger(java.lang.Object value)Returns a formatter Logger using the fully qualified name of the value's Class as the Logger name.static LoggergetFormatterLogger(java.lang.String name)Returns a formatter Logger with the specified name.static LoggergetLogger()Returns a Logger with the name of the calling class.static LoggergetLogger(java.lang.Class<?> clazz)Returns a Logger using the fully qualified name of the Class as the Logger name.static LoggergetLogger(java.lang.Class<?> clazz, MessageFactory messageFactory)Returns a Logger using the fully qualified name of the Class as the Logger name.static LoggergetLogger(java.lang.Object value)Returns a Logger using the fully qualified class name of the value as the Logger name.static LoggergetLogger(java.lang.Object value, MessageFactory messageFactory)Returns a Logger using the fully qualified class name of the value as the Logger name.static LoggergetLogger(java.lang.String name)Returns a Logger with the specified name.static LoggergetLogger(java.lang.String name, MessageFactory messageFactory)Returns a Logger with the specified name.static LoggergetLogger(MessageFactory messageFactory)Returns a Logger with the name of the calling class.static LoggergetRootLogger()Returns the root logger.static voidsetFactory(LoggerContextFactory factory)Sets the current LoggerContextFactory to use.static voidshutdown()Shutdown using the LoggerContext appropriate for the caller of this method.static voidshutdown(boolean currentContext)Shutdown the logging system if the logging system supports it.static voidshutdown(boolean currentContext, boolean allContexts)Shutdown the logging system if the logging system supports it.static voidshutdown(LoggerContext context)Shutdown the logging system if the logging system supports it.
-
-
-
Field Detail
-
FACTORY_PROPERTY_NAME
public static final java.lang.String FACTORY_PROPERTY_NAME
Log4j property to set to the fully qualified class name of a custom implementation ofLoggerContextFactory.- See Also:
- Constant Field Values
-
ROOT_LOGGER_NAME
public static final java.lang.String ROOT_LOGGER_NAME
The name of the root Logger is "".- See Also:
- Constant Field Values
-
-
Method Detail
-
exists
public static boolean exists(java.lang.String name)
Detects if a Logger with the specified name exists. This is a convenience method for porting from version 1.- Parameters:
name- The Logger name to search for.- Returns:
- true if the Logger exists, false otherwise.
- See Also:
LoggerContext.hasLogger(String)
-
getContext
public static LoggerContext getContext()
Returns the current LoggerContext.WARNING - The LoggerContext returned by this method may not be the LoggerContext used to create a Logger for the calling class.
- Returns:
- The current LoggerContext.
-
getContext
public static LoggerContext getContext(boolean currentContext)
Returns a LoggerContext.- Parameters:
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.- Returns:
- a LoggerContext.
-
getContext
public static LoggerContext getContext(java.lang.ClassLoader loader, boolean currentContext)
Returns a LoggerContext.- Parameters:
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.- Returns:
- a LoggerContext.
-
getContext
public static LoggerContext getContext(java.lang.ClassLoader loader, boolean currentContext, java.lang.Object externalContext)
Returns a LoggerContext.- Parameters:
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.- Returns:
- a LoggerContext.
-
getContext
public static LoggerContext getContext(java.lang.ClassLoader loader, boolean currentContext, java.net.URI configLocation)
Returns a LoggerContext.- Parameters:
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.- Returns:
- a LoggerContext.
-
getContext
public static LoggerContext getContext(java.lang.ClassLoader loader, boolean currentContext, java.lang.Object externalContext, java.net.URI configLocation)
Returns a LoggerContext.- Parameters:
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.- Returns:
- a LoggerContext.
-
getContext
public static LoggerContext getContext(java.lang.ClassLoader loader, boolean currentContext, java.lang.Object externalContext, java.net.URI configLocation, java.lang.String name)
Returns a LoggerContext.- Parameters:
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.- Returns:
- a LoggerContext.
-
shutdown
public static void shutdown()
Shutdown using the LoggerContext appropriate for the caller of this method. This is equivalent to callingLogManager.shutdown(false). This call is synchronous and will block until shut down is complete. This may include flushing pending log events over network connections.- Since:
- 2.6
-
shutdown
public static void shutdown(boolean currentContext)
Shutdown the logging system if the logging system supports it. This is equivalent to callingLogManager.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.- Parameters:
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.- Since:
- 2.6
-
shutdown
public static void shutdown(boolean currentContext, boolean allContexts)Shutdown the logging system if the logging system supports it. This is equivalent to callingLogManager.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.- Parameters:
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.- Since:
- 2.13.0
-
shutdown
public static void shutdown(LoggerContext context)
Shutdown the logging system if the logging system supports it. This call is synchronous and will block until shut down is complete. This may include flushing pending log events over network connections.- Parameters:
context- the LoggerContext.- Since:
- 2.6
-
getFactory
public static LoggerContextFactory getFactory()
Returns the current LoggerContextFactory.- Returns:
- The LoggerContextFactory.
-
setFactory
public static void setFactory(LoggerContextFactory factory)
Sets the current LoggerContextFactory to use. Normally, the appropriate LoggerContextFactory is created at startup, but in certain environments, a LoggerContextFactory implementation may not be available at this point. Thus, an alternative LoggerContextFactory can be set at runtime.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.
- Parameters:
factory- the LoggerContextFactory to use.
-
getFormatterLogger
public static Logger getFormatterLogger()
Returns a formatter Logger using the fully qualified name of the calling Class as the Logger name.This logger lets you use a
Formatterstring in the message to format parameters.- Returns:
- The Logger for the calling class.
- Throws:
java.lang.UnsupportedOperationException- if the calling class cannot be determined.- Since:
- 2.4
-
getFormatterLogger
public static Logger getFormatterLogger(java.lang.Class<?> clazz)
Returns a formatter Logger using the fully qualified name of the Class as the Logger name.This logger let you use a
Formatterstring in the message to format parameters.Short-hand for
getLogger(clazz, StringFormatterMessageFactory.INSTANCE)- Parameters:
clazz- The Class whose name should be used as the Logger name.- Returns:
- The Logger, created with a
StringFormatterMessageFactory - Throws:
java.lang.UnsupportedOperationException- ifclazzisnulland the calling class cannot be determined.- See Also:
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
-
getFormatterLogger
public static Logger getFormatterLogger(java.lang.Object value)
Returns a formatter Logger using the fully qualified name of the value's Class as the Logger name.This logger let you use a
Formatterstring in the message to format parameters.Short-hand for
getLogger(value, StringFormatterMessageFactory.INSTANCE)- Parameters:
value- The value's whose class name should be used as the Logger name.- Returns:
- The Logger, created with a
StringFormatterMessageFactory - Throws:
java.lang.UnsupportedOperationException- ifvalueisnulland the calling class cannot be determined.- See Also:
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
-
getFormatterLogger
public static Logger getFormatterLogger(java.lang.String name)
Returns a formatter Logger with the specified name.This logger let you use a
Formatterstring in the message to format parameters.Short-hand for
getLogger(name, StringFormatterMessageFactory.INSTANCE)- Parameters:
name- The logger name. If null it will default to the name of the calling class.- Returns:
- The Logger, created with a
StringFormatterMessageFactory - Throws:
java.lang.UnsupportedOperationException- ifnameisnulland the calling class cannot be determined.- See Also:
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
-
getLogger
public static Logger getLogger()
Returns a Logger with the name of the calling class.- Returns:
- The Logger for the calling class.
- Throws:
java.lang.UnsupportedOperationException- if the calling class cannot be determined.
-
getLogger
public static Logger getLogger(java.lang.Class<?> clazz)
Returns a Logger using the fully qualified name of the Class as the Logger name.- Parameters:
clazz- The Class whose name should be used as the Logger name. If null it will default to the calling class.- Returns:
- The Logger.
- Throws:
java.lang.UnsupportedOperationException- ifclazzisnulland the calling class cannot be determined.
-
getLogger
public static Logger getLogger(java.lang.Class<?> clazz, MessageFactory messageFactory)
Returns a Logger using the fully qualified name of the Class as the Logger name.- Parameters:
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.- Returns:
- The Logger.
- Throws:
java.lang.UnsupportedOperationException- ifclazzisnulland the calling class cannot be determined.
-
getLogger
public static Logger getLogger(MessageFactory messageFactory)
Returns a Logger with the name of the calling class.- Parameters:
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.- Returns:
- The Logger for the calling class.
- Throws:
java.lang.UnsupportedOperationException- if the calling class cannot be determined.
-
getLogger
public static Logger getLogger(java.lang.Object value)
Returns a Logger using the fully qualified class name of the value as the Logger name.- Parameters:
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.- Returns:
- The Logger.
- Throws:
java.lang.UnsupportedOperationException- ifvalueisnulland the calling class cannot be determined.
-
getLogger
public static Logger getLogger(java.lang.Object value, MessageFactory messageFactory)
Returns a Logger using the fully qualified class name of the value as the Logger name.- Parameters:
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.- Returns:
- The Logger.
- Throws:
java.lang.UnsupportedOperationException- ifvalueisnulland the calling class cannot be determined.
-
getLogger
public static Logger getLogger(java.lang.String name)
Returns a Logger with the specified name.- Parameters:
name- The logger name. If null the name of the calling class will be used.- Returns:
- The Logger.
- Throws:
java.lang.UnsupportedOperationException- ifnameisnulland the calling class cannot be determined.
-
getLogger
public static Logger getLogger(java.lang.String name, MessageFactory messageFactory)
Returns a Logger with the specified name.- Parameters:
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.- Returns:
- The Logger.
- Throws:
java.lang.UnsupportedOperationException- ifnameisnulland the calling class cannot be determined.
-
getRootLogger
public static Logger getRootLogger()
Returns the root logger.- Returns:
- the root logger, named
ROOT_LOGGER_NAME.
-
-