Class LoggerContext
- All Implemented Interfaces:
AutoCloseable,ConfigurationListener,LifeCycle,LifeCycle2,LoggerContext,LoggerContextShutdownEnabled,Terminable
- Direct Known Subclasses:
AsyncLoggerContext
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.logging.log4j.core.LifeCycle
LifeCycle.State -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringProperty name of the property change event fired if the configuration is changed.Fields inherited from class org.apache.logging.log4j.core.AbstractLifeCycle
DEFAULT_STOP_TIMEOUT, DEFAULT_STOP_TIMEUNIT, LOGGERFields inherited from interface org.apache.logging.log4j.spi.LoggerContext
EMPTY_ARRAY -
Constructor Summary
ConstructorsConstructorDescriptionLoggerContext(String name) Constructor taking only a name.LoggerContext(String name, Object externalContext) Constructor taking a name and a reference to an external context.LoggerContext(String name, Object externalContext, String configLocn) Constructor taking a name external context and a configuration location String.LoggerContext(String name, Object externalContext, URI configLocn) Constructor taking a name, external context and a configuration URI. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a Filter to the Configuration.voidvoidvoidclose()Returns the initial configuration location ornull.Returns the current Configuration.static LoggerContextReturns the current LoggerContext.static LoggerContextgetContext(boolean currentContext) Returns a LoggerContext.static LoggerContextgetContext(ClassLoader loader, boolean currentContext, URI configLocation) Returns a LoggerContext.Returns the external context.Gets a Logger from the Context.getLogger(String name, @Nullable MessageFactory messageFactory) Obtains a logger from the context.Deprecated.since 2.25.0 without a replacement.Gets a collection of the current loggers.getName()Gets the name.Gets the root logger.booleanDetermines if the specified Logger exists.booleanhasLogger(String name, @Nullable MessageFactory messageFactory) Determines if the specified Logger exists.booleanhasLogger(String name, Class<? extends MessageFactory> messageFactoryClass) Determines if the specified Logger exists.protected LoggernewInstance(LoggerContext context, String name, MessageFactory messageFactory) Callback to create a new logger.voidonChange(Reconfigurable reconfigurable) Causes a reconfiguration to take place when the underlying configuration file changes.putObjectIfAbsent(String key, Object value) voidReconfigures the context.voidreconfigure(Configuration configuration) voidremoveFilter(Filter filter) Removes a Filter from the current Configuration.removeObject(String key) booleanremoveObject(String key, Object value) voidvoidsetConfigLocation(URI configLocation) Sets the configLocation to the specified value and reconfigures this context.setConfiguration(Configuration config) Sets the Configuration to be used.voidsetExternalContext(Object context) Sets the external context.voidSets the name.voidstart()Starts the context using the configuration specified bygetConfigLocation().voidstart(Configuration config) Starts the context using a specific configuration.booleanBlocks 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.voidvoidCauses all Loggers to be updated against the current Configuration.voidupdateLoggers(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, stopMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.logging.log4j.spi.LoggerContext
getLogger, getLogger
-
Field Details
-
PROPERTY_CONFIG
Property name of the property change event fired if the configuration is changed.- See Also:
-
-
Constructor Details
-
LoggerContext
Constructor taking only a name.- Parameters:
name- The context name.
-
LoggerContext
Constructor taking a name and a reference to an external context.- Parameters:
name- The context name.externalContext- The external context.
-
LoggerContext
Constructor taking a name, external context and a configuration URI.- Parameters:
name- The context name.externalContext- The external context.configLocn- The location of the configuration as a URI.
-
LoggerContext
Constructor taking a name external context and a configuration location String. The location must be resolvable to a File.- Parameters:
name- The configuration location.externalContext- The external context.configLocn- The configuration location.
-
-
Method Details
-
addShutdownListener
- Specified by:
addShutdownListenerin interfaceLoggerContextShutdownEnabled
-
getListeners
- Specified by:
getListenersin interfaceLoggerContextShutdownEnabled
-
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:
-
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:
-
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:
-
start
public void start()Starts the context using the configuration specified bygetConfigLocation().If the configuration location is
null, Log4j will search for a configuration file using the default classpath resources. For details on the search order and supported formats, see the Log4j 2 Configuration File Location documentation.- Specified by:
startin interfaceLifeCycle- Overrides:
startin classAbstractLifeCycle
-
start
Starts the context using a specific configuration.Warning: For backward compatibility, especially with Spring Boot, if the context is already started, this method will fall back to
reconfigure(Configuration). This behavior is maintained for legacy integrations and may change in future major versions. New code should not rely on this fallback.- Parameters:
config- The newConfigurationto use for this context
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
terminate
public void terminate()- Specified by:
terminatein interfaceTerminable
-
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:
stopin interfaceLifeCycle2- Overrides:
stopin 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:
trueif the logger context terminated andfalseif 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:
getObjectin interfaceLoggerContext
-
putObject
- Specified by:
putObjectin interfaceLoggerContext
-
putObjectIfAbsent
- Specified by:
putObjectIfAbsentin interfaceLoggerContext
-
removeObject
- Specified by:
removeObjectin interfaceLoggerContext
-
removeObject
- Specified by:
removeObjectin interfaceLoggerContext
-
setExternalContext
Sets the external context.- Parameters:
context- The external context.
-
getExternalContext
Returns the external context.- Specified by:
getExternalContextin interfaceLoggerContext- Returns:
- The external context.
-
getLogger
Gets a Logger from the Context.- Specified by:
getLoggerin interfaceLoggerContext- 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
Obtains a logger from the context.- Specified by:
getLoggerin interfaceLoggerContext- Parameters:
name- a logger namemessageFactory- a message factory to associate the logger with- Returns:
- a logger matching the given name and message factory
-
getLoggerRegistry
Deprecated.since 2.25.0 without a replacement.Gets the LoggerRegistry.- Specified by:
getLoggerRegistryin interfaceLoggerContext- Returns:
- the LoggerRegistry.
- Since:
- 2.17.2
-
hasLogger
Determines if the specified Logger exists.- Specified by:
hasLoggerin interfaceLoggerContext- Parameters:
name- The Logger name to search for.- Returns:
- True if the Logger exists, false otherwise.
-
hasLogger
Determines if the specified Logger exists.- Specified by:
hasLoggerin interfaceLoggerContext- Parameters:
name- The Logger name to search for.- Returns:
- True if the Logger exists, false otherwise.
-
hasLogger
Determines if the specified Logger exists.- Specified by:
hasLoggerin interfaceLoggerContext- 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.
-
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.
-
addPropertyChangeListener
-
removePropertyChangeListener
-
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.
-
onChange
Causes a reconfiguration to take place when the underlying configuration file changes.- Specified by:
onChangein interfaceConfigurationListener- Parameters:
reconfigurable- The Configuration that can be reconfigured.
-
newInstance
Callback to create a new logger.- Parameters:
context- TheLoggerContextthis logger is associated with, nevernull.messageFactory- The message factory to be used, nevernull.name- The logger name, nevernull.- Returns:
- A new logger instance.
-