org.apache.logging.log4j.core
Class LoggerContext

java.lang.Object
  extended by org.apache.logging.log4j.core.LoggerContext
All Implemented Interfaces:
ConfigurationListener, LifeCycle, org.apache.logging.log4j.spi.LoggerContext
Direct Known Subclasses:
AsyncLoggerContext

public class LoggerContext
extends Object
implements org.apache.logging.log4j.spi.LoggerContext, ConfigurationListener, LifeCycle

The LoggerContext is the anchor for the logging system. It maintains a list of all the loggers requested by applications and a reference to the Configuration. The Configuration will contain the configured loggers, appenders, filters, etc and will be atomically updated whenever a reconfigure occurs.


Nested Class Summary
static class LoggerContext.Status
          Status of the LoggerContext.
 
Field Summary
static String PROPERTY_CONFIG
           
 
Constructor Summary
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.
 
Method Summary
 void addFilter(Filter filter)
          Add a Filter to the Configuration.
 void addPropertyChangeListener(PropertyChangeListener listener)
           
 URI getConfigLocation()
           
 Configuration getConfiguration()
          Returns the current Configuration.
 Object getExternalContext()
          Returns the external context.
 Logger getLogger(String name)
          Obtain a Logger from the Context.
 Logger getLogger(String name, org.apache.logging.log4j.message.MessageFactory messageFactory)
          Obtain a Logger from the Context.
 String getName()
          Gets the name.
 LoggerContext.Status getStatus()
           
 boolean hasLogger(String name)
          Determine if the specified Logger exists.
 boolean isStarted()
           
protected  Logger newInstance(LoggerContext ctx, String name, org.apache.logging.log4j.message.MessageFactory messageFactory)
           
 void onChange(Reconfigurable reconfigurable)
          Cause a reconfiguration to take place when the underlying configuration file changes.
 void reconfigure()
          Reconfigure the context.
 void removeFilter(Filter filter)
          Removes a Filter from the current Configuration.
 void removePropertyChangeListener(PropertyChangeListener listener)
           
 void setConfigLocation(URI configLocation)
           
 Configuration setConfiguration(Configuration config)
          Set the Configuration to be used.
 void setExternalContext(Object context)
          Set the external context.
 void start()
           
 void stop()
           
 void updateLoggers()
          Cause all Loggers to be updated against the current Configuration.
 void updateLoggers(Configuration config)
          Cause all Logger to be updated against the specified Configuration.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPERTY_CONFIG

public static final String PROPERTY_CONFIG
See Also:
Constant Field Values
Constructor Detail

LoggerContext

public LoggerContext(String name)
Constructor taking only a name.

Parameters:
name - The context name.

LoggerContext

public LoggerContext(String name,
                     Object externalContext)
Constructor taking a name and a reference to an external context.

Parameters:
name - The context name.
externalContext - The external context.

LoggerContext

public LoggerContext(String name,
                     Object externalContext,
                     URI configLocn)
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

public LoggerContext(String name,
                     Object externalContext,
                     String configLocn)
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 Detail

start

public void start()
Specified by:
start in interface LifeCycle

stop

public void stop()
Specified by:
stop in interface LifeCycle

getName

public String getName()
Gets the name.

Returns:
the name.

getStatus

public LoggerContext.Status getStatus()

isStarted

public boolean isStarted()
Specified by:
isStarted in interface LifeCycle

setExternalContext

public void setExternalContext(Object context)
Set the external context.

Parameters:
context - The external context.

getExternalContext

public Object getExternalContext()
Returns the external context.

Specified by:
getExternalContext in interface org.apache.logging.log4j.spi.LoggerContext
Returns:
The external context.

getLogger

public Logger getLogger(String name)
Obtain a Logger from the Context.

Specified by:
getLogger in interface org.apache.logging.log4j.spi.LoggerContext
Parameters:
name - The name of the Logger to return.
Returns:
The Logger.

getLogger

public Logger getLogger(String name,
                        org.apache.logging.log4j.message.MessageFactory messageFactory)
Obtain a Logger from the Context.

Specified by:
getLogger in interface org.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.

hasLogger

public boolean hasLogger(String name)
Determine if the specified Logger exists.

Specified by:
hasLogger in interface org.apache.logging.log4j.spi.LoggerContext
Parameters:
name - The Logger name to search for.
Returns:
True if the Logger exists, false otherwise.

getConfiguration

public Configuration getConfiguration()
Returns the current Configuration. The Configuration will be replaced when a reconfigure occurs.

Returns:
The Configuration.

addFilter

public void addFilter(Filter filter)
Add 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

public void removeFilter(Filter filter)
Removes a Filter from the current Configuration.

Parameters:
filter - The Filter to remove.

setConfiguration

public Configuration setConfiguration(Configuration config)
Set the Configuration to be used.

Parameters:
config - The new Configuration.
Returns:
The previous Configuration.

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)

getConfigLocation

public URI getConfigLocation()

setConfigLocation

public void setConfigLocation(URI configLocation)

reconfigure

public void reconfigure()
Reconfigure the context.


updateLoggers

public void updateLoggers()
Cause all Loggers to be updated against the current Configuration.


updateLoggers

public void updateLoggers(Configuration config)
Cause all Logger to be updated against the specified Configuration.

Parameters:
config - The Configuration.

onChange

public void onChange(Reconfigurable reconfigurable)
Cause a reconfiguration to take place when the underlying configuration file changes.

Specified by:
onChange in interface ConfigurationListener
Parameters:
reconfigurable - The Configuration that can be reconfigured.

newInstance

protected Logger newInstance(LoggerContext ctx,
                             String name,
                             org.apache.logging.log4j.message.MessageFactory messageFactory)


Copyright © 1999-2013 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.