Class AbstractManager
- java.lang.Object
-
- org.apache.logging.log4j.core.appender.AbstractManager
-
- All Implemented Interfaces:
java.lang.AutoCloseable
- Direct Known Subclasses:
AbstractDatabaseManager,HttpManager,JeroMqManager,JmsManager,JndiManager,MailManager,OutputStreamManager,WriterManager
public abstract class AbstractManager extends java.lang.Object implements java.lang.AutoCloseableAbstract base class used to register managers.This class implements
AutoCloseablemostly to allow unit tests to be written safely and succinctly. While managers do need to allocate resources (usually on construction) and then free these resources, a manager is longer lived than other auto-closeable objects like streams. None the less, making a manager AutoCloseable forces readers to be aware of the pattern: allocate resources on construction and callclose()at some point.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAbstractManager.AbstractFactoryDataImplementations should extend this class for passing data between the getManager method and the manager factory class.
-
Field Summary
Fields Modifier and Type Field Description protected intcountNumber of Appenders using this manager.private static java.util.concurrent.locks.LockLOCKprotected static LoggerLOGGERAllow subclasses access to the status logger without creating another instance.private LoggerContextloggerContextprivate static java.util.Map<java.lang.String,AbstractManager>MAPprivate java.lang.Stringname
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractManager(LoggerContext loggerContext, java.lang.String name)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclose()Called to signify that this Manager is no longer required by an Appender.java.util.Map<java.lang.String,java.lang.String>getContentFormat()Provide a description of the content format supported by this Manager.protected intgetCount()LoggerContextgetLoggerContext()Gets the logger context used to create this instance or null.static <M extends AbstractManager,T>
MgetManager(java.lang.String name, ManagerFactory<M,T> factory, T data)Retrieves a Manager if it has been previously created or creates a new Manager.(package private) static intgetManagerCount()For testing purposes.java.lang.StringgetName()Returns the name of the Manager.protected StrSubstitutorgetStrSubstitutor()Gets my configuration's StrSubstitutor or null.static booleanhasManager(java.lang.String name)Determines if a Manager with the specified name exists.protected voidlog(Level level, java.lang.String message, java.lang.Throwable throwable)protected voidlogDebug(java.lang.String message, java.lang.Throwable throwable)protected voidlogError(java.lang.String message, java.lang.Throwable throwable)protected static StatusLoggerlogger()protected voidlogWarn(java.lang.String message, java.lang.Throwable throwable)protected static <M extends AbstractManager>
Mnarrow(java.lang.Class<M> narrowClass, AbstractManager manager)Returns the specified manager, cast to the specified narrow type.voidrelease()Deprecated.In 2.7, useclose().protected booleanreleaseSub(long timeout, java.util.concurrent.TimeUnit timeUnit)May be overridden by managers to perform processing while the manager is being released and the lock is held.booleanstop(long timeout, java.util.concurrent.TimeUnit timeUnit)voidupdateData(java.lang.Object data)Used by Log4j to update the Manager during reconfiguration.
-
-
-
Field Detail
-
LOGGER
protected static final Logger LOGGER
Allow subclasses access to the status logger without creating another instance.
-
MAP
private static final java.util.Map<java.lang.String,AbstractManager> MAP
-
LOCK
private static final java.util.concurrent.locks.Lock LOCK
-
count
protected int count
Number of Appenders using this manager.
-
name
private final java.lang.String name
-
loggerContext
private final LoggerContext loggerContext
-
-
Constructor Detail
-
AbstractManager
protected AbstractManager(LoggerContext loggerContext, java.lang.String name)
-
-
Method Detail
-
close
public void close()
Called to signify that this Manager is no longer required by an Appender.- Specified by:
closein interfacejava.lang.AutoCloseable
-
stop
public boolean stop(long timeout, java.util.concurrent.TimeUnit timeUnit)
-
getManager
public static <M extends AbstractManager,T> M getManager(java.lang.String name, ManagerFactory<M,T> factory, T data)
Retrieves a Manager if it has been previously created or creates a new Manager.- Type Parameters:
M- The Type of the Manager to be created.T- The type of the Factory data.- Parameters:
name- The name of the Manager to retrieve.factory- The Factory to use to create the Manager.data- An Object that should be passed to the factory when creating the Manager.- Returns:
- A Manager with the specified name and type.
-
updateData
public void updateData(java.lang.Object data)
Used by Log4j to update the Manager during reconfiguration. This method should be considered private. Implementations may not be thread safe. This method may be made protected in a future release.- Parameters:
data- The data to update.
-
hasManager
public static boolean hasManager(java.lang.String name)
Determines if a Manager with the specified name exists.- Parameters:
name- The name of the Manager.- Returns:
- True if the Manager exists, false otherwise.
-
narrow
protected static <M extends AbstractManager> M narrow(java.lang.Class<M> narrowClass, AbstractManager manager)
Returns the specified manager, cast to the specified narrow type.- Type Parameters:
M- the narrow type- Parameters:
narrowClass- the type to cast tomanager- the manager object to return- Returns:
- the specified manager, cast to the specified narrow type
- Throws:
ConfigurationException- if the manager cannot be cast to the specified type, which only happens when the configuration has multiple incompatible appenders pointing to the same resource- Since:
- 2.9
- See Also:
- LOG4J2-1908
-
logger
protected static StatusLogger logger()
-
getManagerCount
static int getManagerCount()
For testing purposes.
-
releaseSub
protected boolean releaseSub(long timeout, java.util.concurrent.TimeUnit timeUnit)May be overridden by managers to perform processing while the manager is being released and the lock is held. A timeout is passed for implementors to use as they see fit.- Parameters:
timeout- timeouttimeUnit- timeout time unit- Returns:
- true if all resources were closed normally, false otherwise.
-
getCount
protected int getCount()
-
getLoggerContext
public LoggerContext getLoggerContext()
Gets the logger context used to create this instance or null. The logger context is usually set when an appender creates a manager and that appender is given a Configuration. Not all appenders are given a Configuration by their factory method or builder.- Returns:
- the logger context used to create this instance or null.
-
release
@Deprecated public void release()
Deprecated.In 2.7, useclose().Called to signify that this Manager is no longer required by an Appender.
-
getName
public java.lang.String getName()
Returns the name of the Manager.- Returns:
- The name of the Manager.
-
getContentFormat
public java.util.Map<java.lang.String,java.lang.String> getContentFormat()
Provide a description of the content format supported by this Manager. Default implementation returns an empty (unspecified) Map.- Returns:
- a Map of key/value pairs describing the Manager-specific content format, or an empty Map if no content format descriptors are specified.
-
getStrSubstitutor
protected StrSubstitutor getStrSubstitutor()
Gets my configuration's StrSubstitutor or null.- Returns:
- my configuration's StrSubstitutor or null.
-
log
protected void log(Level level, java.lang.String message, java.lang.Throwable throwable)
-
logDebug
protected void logDebug(java.lang.String message, java.lang.Throwable throwable)
-
logError
protected void logError(java.lang.String message, java.lang.Throwable throwable)
-
logWarn
protected void logWarn(java.lang.String message, java.lang.Throwable throwable)
-
-