Class LoggerContext
- All Implemented Interfaces:
AutoCloseable
,Consumer<Reconfigurable>
,LifeCycle
,org.apache.logging.log4j.spi.LoggerContext
,org.apache.logging.log4j.spi.LoggerContextShutdownEnabled
,org.apache.logging.log4j.spi.Terminable
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface org.apache.logging.log4j.core.LifeCycle
LifeCycle.State
-
Field Summary
FieldsFields inherited from class org.apache.logging.log4j.core.AbstractLifeCycle
DEFAULT_STOP_TIMEOUT, DEFAULT_STOP_TIMEUNIT, LOGGER
Fields inherited from interface org.apache.logging.log4j.spi.LoggerContext
EMPTY_ARRAY
-
Constructor Summary
ConstructorsConstructorDescriptionLoggerContext
(String name, @Nullable Object externalContext, @Nullable URI configLocation, ConfigurableInstanceFactory instanceFactory) Constructs a LoggerContext with a name, external context, configuration URI, and a ConfigurableInstanceFactory.LoggerContext
(String contextName, Object externalContext, String configLocation, ConfigurableInstanceFactory instanceFactory) Constructs a LoggerContext with a name, external context, configuration location string, and an instance factory. -
Method Summary
Modifier and TypeMethodDescriptionvoid
accept
(Reconfigurable reconfigurable) Causes a reconfiguration to take place when the underlying configuration file changes.void
addConfigurationStartedListener
(Consumer<Configuration> listener) void
addConfigurationStoppedListener
(Consumer<Configuration> listener) void
Adds a Filter to the Configuration.void
addShutdownListener
(org.apache.logging.log4j.spi.LoggerContextShutdownAware listener) void
close()
Returns the initial configuration location ornull
.Returns the current Configuration.getConfiguration
(String name, URI configLocation) getConfiguration
(String name, URI configLocation, ClassLoader loader) getConfiguration
(ConfigurationSource source) static LoggerContext
Returns the current LoggerContext.static LoggerContext
getContext
(boolean currentContext) Returns a LoggerContext.static LoggerContext
getContext
(ClassLoader loader, boolean currentContext, URI configLocation) Returns a LoggerContext.Returns the external context.Gets the InstanceFactory.List<org.apache.logging.log4j.spi.LoggerContextShutdownAware>
Gets a Logger from the Context.Obtains a Logger from the Context.protected Class<? extends Logger.Builder>
org.apache.logging.log4j.spi.LoggerRegistry<Logger>
Gets the LoggerRegistry.Gets a collection of the current loggers.getName()
Gets the name.Gets the root logger.boolean
Determines if the specified Logger exists.boolean
hasLogger
(String name, Class<? extends org.apache.logging.log4j.message.MessageFactory> messageFactoryClass) Determines if the specified Logger exists.boolean
Determines if the specified Logger exists.boolean
Iftrue
loggers will include location by default.putObjectIfAbsent
(String key, Object value) void
Reconfigures the context.void
reconfigure
(Configuration configuration) void
void
void
removeFilter
(Filter filter) Removes a Filter from the current Configuration.removeObject
(String key) boolean
removeObject
(String key, Object value) void
setConfigLocation
(URI configLocation) Sets the configLocation to the specified value and reconfigures this context.setConfiguration
(Configuration config) Sets the Configuration to be used.void
setExternalContext
(Object context) Sets the external context.void
Sets the name.void
start()
void
start
(Configuration config) Starts with a specific configuration.boolean
Blocks until all Log4j tasks have completed execution after a shutdown request and all appenders have shut down, or the timeout occurs, or the current thread is interrupted, whichever happens first.void
toString()
void
Causes all Loggers to be updated against the current Configuration.void
updateLoggers
(Configuration config) Causes all Logger to be updated against the specified Configuration.Methods inherited from class org.apache.logging.log4j.core.AbstractLifeCycle
equalsImpl, getState, getStatusLogger, hashCodeImpl, initialize, isInitialized, isStarted, isStarting, isStopped, isStopping, setStarted, setStarting, setState, setStopped, setStopping, stop, stop
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.apache.logging.log4j.spi.LoggerContext
getLogger, getLogger
-
Field Details
-
KEY
-
-
Constructor Details
-
LoggerContext
public LoggerContext(String name, @Nullable Object externalContext, @Nullable URI configLocation, ConfigurableInstanceFactory instanceFactory) Constructs a LoggerContext with a name, external context, configuration URI, and a ConfigurableInstanceFactory.- Parameters:
name
- context nameexternalContext
- external context or nullconfigLocation
- location of configuration as a URIinstanceFactory
- initialized ConfigurableInstanceFactory
-
LoggerContext
public LoggerContext(String contextName, Object externalContext, String configLocation, ConfigurableInstanceFactory instanceFactory) Constructs a LoggerContext with a name, external context, configuration location string, and an instance factory. The location must be resolvable to a File.- Parameters:
contextName
- context nameexternalContext
- external context or nullconfigLocation
- configuration locationinstanceFactory
- initialized ConfigurableInstanceFactory
-
-
Method Details
-
getEnvironment
-
addShutdownListener
public void addShutdownListener(org.apache.logging.log4j.spi.LoggerContextShutdownAware listener) - Specified by:
addShutdownListener
in interfaceorg.apache.logging.log4j.spi.LoggerContextShutdownEnabled
-
getListeners
- Specified by:
getListeners
in interfaceorg.apache.logging.log4j.spi.LoggerContextShutdownEnabled
-
getContext
Returns the current LoggerContext.Avoids the type cast for:
(LoggerContext) LogManager.getContext();
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.
- See Also:
-
LogManager.getContext()
-
getContext
Returns a LoggerContext.Avoids the type cast for:
(LoggerContext) LogManager.getContext(currentContext);
- 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.
- See Also:
-
LogManager.getContext(boolean)
-
getContext
public static LoggerContext getContext(ClassLoader loader, boolean currentContext, URI configLocation) Returns a LoggerContext.Avoids the type cast for:
(LoggerContext) LogManager.getContext(loader, currentContext, configLocation);
- 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.
- See Also:
-
LogManager.getContext(ClassLoader, boolean, URI)
-
start
public void start()- Specified by:
start
in interfaceLifeCycle
- Overrides:
start
in classAbstractLifeCycle
-
start
Starts with a specific configuration.- Parameters:
config
- The new Configuration.
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-
terminate
public void terminate()- Specified by:
terminate
in interfaceorg.apache.logging.log4j.spi.Terminable
-
stop
Blocks until all Log4j tasks have completed execution after a shutdown request and all appenders have shut down, or the timeout occurs, or the current thread is interrupted, whichever happens first.Not all appenders will honor this, it is a hint and not an absolute guarantee that the this method not block longer. Setting timeout too low increase the risk of losing outstanding log events not yet written to the final destination.
Log4j can start threads to perform certain actions like file rollovers, calling this method with a positive timeout will block until the rollover thread is done.
- Specified by:
stop
in interfaceLifeCycle
- Overrides:
stop
in classAbstractLifeCycle
- Parameters:
timeout
- the maximum time to wait, or 0 which mean that each apppender uses its default timeout, and don't wait for background taskstimeUnit
- the time unit of the timeout argument- Returns:
true
if the logger context terminated andfalse
if the timeout elapsed before termination.- Since:
- 2.7
-
getName
Gets the name.- Returns:
- the name.
-
getRootLogger
Gets the root logger.- Returns:
- the root logger.
-
setName
Sets the name.- Parameters:
name
- the new LoggerContext name- Throws:
NullPointerException
- if the specified name isnull
-
getObject
- Specified by:
getObject
in interfaceorg.apache.logging.log4j.spi.LoggerContext
-
putObject
- Specified by:
putObject
in interfaceorg.apache.logging.log4j.spi.LoggerContext
-
putObjectIfAbsent
- Specified by:
putObjectIfAbsent
in interfaceorg.apache.logging.log4j.spi.LoggerContext
-
removeObject
- Specified by:
removeObject
in interfaceorg.apache.logging.log4j.spi.LoggerContext
-
removeObject
- Specified by:
removeObject
in interfaceorg.apache.logging.log4j.spi.LoggerContext
-
setExternalContext
Sets the external context.- Parameters:
context
- The external context.
-
getExternalContext
Returns the external context.- Specified by:
getExternalContext
in interfaceorg.apache.logging.log4j.spi.LoggerContext
- Returns:
- The external context.
-
getLogger
Gets a Logger from the Context.- Specified by:
getLogger
in interfaceorg.apache.logging.log4j.spi.LoggerContext
- Parameters:
name
- The name of the Logger to return.- Returns:
- The Logger.
-
getLoggers
Gets a collection of the current loggers.Whether this collection is a copy of the underlying collection or not is undefined. Therefore, modify this collection at your own risk.
- Returns:
- a collection of the current loggers.
-
getLogger
public Logger getLogger(String name, org.apache.logging.log4j.message.MessageFactory messageFactory) Obtains a Logger from the Context.- Specified by:
getLogger
in interfaceorg.apache.logging.log4j.spi.LoggerContext
- Parameters:
name
- The name of the Logger to return.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.
-
getLoggerRegistry
Gets the LoggerRegistry.- Specified by:
getLoggerRegistry
in interfaceorg.apache.logging.log4j.spi.LoggerContext
- Returns:
- the LoggerRegistry.
- Since:
- 2.17.2
-
getInstanceFactory
Gets the InstanceFactory.- Returns:
- the InstanceFactory
- Since:
- 3.0.0
-
hasLogger
Determines if the specified Logger exists.- Specified by:
hasLogger
in interfaceorg.apache.logging.log4j.spi.LoggerContext
- Parameters:
name
- The Logger name to search for.- Returns:
- True if the Logger exists, false otherwise.
-
hasLogger
public boolean hasLogger(String name, org.apache.logging.log4j.message.MessageFactory messageFactory) Determines if the specified Logger exists.- Specified by:
hasLogger
in interfaceorg.apache.logging.log4j.spi.LoggerContext
- Parameters:
name
- The Logger name to search for.- Returns:
- True if the Logger exists, false otherwise.
-
hasLogger
public boolean hasLogger(String name, Class<? extends org.apache.logging.log4j.message.MessageFactory> messageFactoryClass) Determines if the specified Logger exists.- Specified by:
hasLogger
in interfaceorg.apache.logging.log4j.spi.LoggerContext
- Parameters:
name
- The Logger name to search for.- Returns:
- True if the Logger exists, false otherwise.
-
getConfiguration
Returns the current Configuration. The Configuration will be replaced when a reconfigure occurs.- Returns:
- The current Configuration, never
null
, but may beNullConfiguration
.
-
getConfiguration
-
getConfiguration
-
getConfiguration
-
addFilter
Adds a Filter to the Configuration. Filters that are added through the API will be lost when a reconfigure occurs.- Parameters:
filter
- The Filter to add.
-
removeFilter
Removes a Filter from the current Configuration.- Parameters:
filter
- The Filter to remove.
-
setConfiguration
Sets the Configuration to be used.- Parameters:
config
- The new Configuration.- Returns:
- The previous Configuration.
-
addConfigurationStartedListener
-
removeConfigurationStartedListener
-
addConfigurationStoppedListener
-
removeConfigurationStoppedListener
-
getConfigLocation
Returns the initial configuration location ornull
. The returned value may not be the location of the current configuration. UsegetConfiguration()
.getConfigurationSource()
.getLocation()
to get the actual source of the current configuration.- Returns:
- the initial configuration location or
null
-
setConfigLocation
Sets the configLocation to the specified value and reconfigures this context.- Parameters:
configLocation
- the location of the new configuration
-
reconfigure
public void reconfigure()Reconfigures the context. Log4j does not remove Loggers during a reconfiguration. Log4j will create new LoggerConfig objects and Log4j will point the Loggers at the new LoggerConfigs. Log4j will free the old LoggerConfig, along with old Appenders and Filters. -
reconfigure
-
updateLoggers
public void updateLoggers()Causes all Loggers to be updated against the current Configuration. -
updateLoggers
Causes all Logger to be updated against the specified Configuration.- Parameters:
config
- The Configuration.
-
accept
Causes a reconfiguration to take place when the underlying configuration file changes.- Specified by:
accept
in interfaceConsumer<Reconfigurable>
- Parameters:
reconfigurable
- The Configuration that can be reconfigured.
-
toString
-
getLoggerBuilderClass
-
includeLocation
public boolean includeLocation()Iftrue
loggers will include location by default.
-