public class LoggerContext extends AbstractLifeCycle implements LoggerContext, AutoCloseable, Terminable, ConfigurationListener, LoggerContextShutdownEnabled
LifeCycle.State
Modifier and Type | Field and Description |
---|---|
static String |
PROPERTY_CONFIG
Property name of the property change event fired if the configuration is changed.
|
DEFAULT_STOP_TIMEOUT, DEFAULT_STOP_TIMEUNIT, LOGGER
EMPTY_ARRAY
Constructor and Description |
---|
LoggerContext(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.
|
Modifier and Type | Method and Description |
---|---|
void |
addFilter(Filter filter)
Adds a Filter to the Configuration.
|
void |
addPropertyChangeListener(PropertyChangeListener listener) |
void |
addShutdownListener(LoggerContextShutdownAware listener) |
void |
close() |
URI |
getConfigLocation()
Returns the initial configuration location or
null . |
Configuration |
getConfiguration()
Returns the current Configuration.
|
static LoggerContext |
getContext()
Returns the current LoggerContext.
|
static LoggerContext |
getContext(boolean currentContext)
Returns a LoggerContext.
|
static LoggerContext |
getContext(ClassLoader loader,
boolean currentContext,
URI configLocation)
Returns a LoggerContext.
|
Object |
getExternalContext()
Returns the external context.
|
List<LoggerContextShutdownAware> |
getListeners() |
Logger |
getLogger(String name)
Gets a Logger from the Context.
|
Logger |
getLogger(String name,
MessageFactory messageFactory)
Obtains a Logger from the Context.
|
LoggerRegistry<Logger> |
getLoggerRegistry()
Gets the LoggerRegistry.
|
Collection<Logger> |
getLoggers()
Gets a collection of the current loggers.
|
String |
getName()
Gets the name.
|
Object |
getObject(String key) |
Logger |
getRootLogger()
Gets the root logger.
|
boolean |
hasLogger(String name)
Determines if the specified Logger exists.
|
boolean |
hasLogger(String name,
Class<? extends MessageFactory> messageFactoryClass)
Determines if the specified Logger exists.
|
boolean |
hasLogger(String name,
MessageFactory messageFactory)
Determines if the specified Logger exists.
|
protected Logger |
newInstance(LoggerContext ctx,
String name,
MessageFactory messageFactory) |
void |
onChange(Reconfigurable reconfigurable)
Causes a reconfiguration to take place when the underlying configuration file changes.
|
Object |
putObject(String key,
Object value) |
Object |
putObjectIfAbsent(String key,
Object value) |
void |
reconfigure()
Reconfigures the context.
|
void |
reconfigure(Configuration configuration) |
void |
removeFilter(Filter filter)
Removes a Filter from the current Configuration.
|
Object |
removeObject(String key) |
boolean |
removeObject(String key,
Object value) |
void |
removePropertyChangeListener(PropertyChangeListener listener) |
void |
setConfigLocation(URI configLocation)
Sets the configLocation to the specified value and reconfigures this context.
|
Configuration |
setConfiguration(Configuration config)
Sets the Configuration to be used.
|
void |
setExternalContext(Object context)
Sets the external context.
|
void |
setName(String name)
Sets the name.
|
void |
start() |
void |
start(Configuration config)
Starts with a specific configuration.
|
boolean |
stop(long timeout,
TimeUnit timeUnit)
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 |
terminate() |
void |
updateLoggers()
Causes all Loggers to be updated against the current Configuration.
|
void |
updateLoggers(Configuration config)
Causes all Logger to be updated against the specified Configuration.
|
equalsImpl, getState, getStatusLogger, hashCodeImpl, initialize, isInitialized, isStarted, isStarting, isStopped, isStopping, setStarted, setStarting, setState, setStopped, setStopping, stop, stop
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getLogger, getLogger
public static final String PROPERTY_CONFIG
public LoggerContext(String name)
name
- The context name.public LoggerContext(String name, Object externalContext)
name
- The context name.externalContext
- The external context.public LoggerContext(String name, Object externalContext, URI configLocn)
name
- The context name.externalContext
- The external context.configLocn
- The location of the configuration as a URI.public LoggerContext(String name, Object externalContext, String configLocn)
name
- The configuration location.externalContext
- The external context.configLocn
- The configuration location.public void addShutdownListener(LoggerContextShutdownAware listener)
addShutdownListener
in interface LoggerContextShutdownEnabled
public List<LoggerContextShutdownAware> getListeners()
getListeners
in interface LoggerContextShutdownEnabled
public static LoggerContext getContext()
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.
LogManager.getContext()
public static LoggerContext getContext(boolean currentContext)
Avoids the type cast for:
(LoggerContext) LogManager.getContext(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.LogManager.getContext(boolean)
public static LoggerContext getContext(ClassLoader loader, boolean currentContext, URI configLocation)
Avoids the type cast for:
(LoggerContext) LogManager.getContext(loader, currentContext, 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.LogManager.getContext(ClassLoader, boolean, URI)
public void start()
start
in interface LifeCycle
start
in class AbstractLifeCycle
public void start(Configuration config)
config
- The new Configuration.public void close()
close
in interface AutoCloseable
public void terminate()
terminate
in interface Terminable
public boolean stop(long timeout, TimeUnit timeUnit)
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.
stop
in interface LifeCycle2
stop
in class AbstractLifeCycle
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 argumenttrue
if the logger context terminated and false
if the timeout elapsed before
termination.public String getName()
public Logger getRootLogger()
public void setName(String name)
name
- the new LoggerContext nameNullPointerException
- if the specified name is null
public Object getObject(String key)
getObject
in interface LoggerContext
public Object putObject(String key, Object value)
putObject
in interface LoggerContext
public Object putObjectIfAbsent(String key, Object value)
putObjectIfAbsent
in interface LoggerContext
public Object removeObject(String key)
removeObject
in interface LoggerContext
public boolean removeObject(String key, Object value)
removeObject
in interface LoggerContext
public void setExternalContext(Object context)
context
- The external context.public Object getExternalContext()
getExternalContext
in interface LoggerContext
public Logger getLogger(String name)
getLogger
in interface LoggerContext
name
- The name of the Logger to return.public Collection<Logger> getLoggers()
Whether this collection is a copy of the underlying collection or not is undefined. Therefore, modify this collection at your own risk.
public Logger getLogger(String name, MessageFactory messageFactory)
getLogger
in interface LoggerContext
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.public LoggerRegistry<Logger> getLoggerRegistry()
getLoggerRegistry
in interface LoggerContext
public boolean hasLogger(String name)
hasLogger
in interface LoggerContext
name
- The Logger name to search for.public boolean hasLogger(String name, MessageFactory messageFactory)
hasLogger
in interface LoggerContext
name
- The Logger name to search for.public boolean hasLogger(String name, Class<? extends MessageFactory> messageFactoryClass)
hasLogger
in interface LoggerContext
name
- The Logger name to search for.public Configuration getConfiguration()
null
, but may be
NullConfiguration
.public void addFilter(Filter filter)
filter
- The Filter to add.public void removeFilter(Filter filter)
filter
- The Filter to remove.public Configuration setConfiguration(Configuration config)
config
- The new Configuration.public void addPropertyChangeListener(PropertyChangeListener listener)
public void removePropertyChangeListener(PropertyChangeListener listener)
public URI getConfigLocation()
null
. The returned value may not be the location of the
current configuration. Use getConfiguration()
.getConfigurationSource()
.getLocation()
to get the actual source of the
current configuration.null
public void setConfigLocation(URI configLocation)
configLocation
- the location of the new configurationpublic void reconfigure()
public void reconfigure(Configuration configuration)
public void updateLoggers()
public void updateLoggers(Configuration config)
config
- The Configuration.public void onChange(Reconfigurable reconfigurable)
onChange
in interface ConfigurationListener
reconfigurable
- The Configuration that can be reconfigured.protected Logger newInstance(LoggerContext ctx, String name, MessageFactory messageFactory)
Copyright © 1999-2023 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.