Class Log4jContextFactory
- java.lang.Object
-
- org.apache.logging.log4j.core.impl.Log4jContextFactory
-
- All Implemented Interfaces:
ShutdownCallbackRegistry,LoggerContextFactory
public class Log4jContextFactory extends java.lang.Object implements LoggerContextFactory, ShutdownCallbackRegistry
Factory to locate a ContextSelector and then load a LoggerContext.
-
-
Field Summary
Fields Modifier and Type Field Description private static StatusLoggerLOGGERprivate ContextSelectorselectorprivate static booleanSHUTDOWN_HOOK_ENABLEDprivate ShutdownCallbackRegistryshutdownCallbackRegistry-
Fields inherited from interface org.apache.logging.log4j.core.util.ShutdownCallbackRegistry
SHUTDOWN_CALLBACK_REGISTRY, SHUTDOWN_HOOK_MARKER
-
-
Constructor Summary
Constructors Constructor Description Log4jContextFactory()Initializes the ContextSelector from system propertyConstants.LOG4J_CONTEXT_SELECTOR.Log4jContextFactory(ContextSelector selector)Initializes this factory's ContextSelector with the specified selector.Log4jContextFactory(ContextSelector selector, ShutdownCallbackRegistry shutdownCallbackRegistry)Constructs a Log4jContextFactory using the provided ContextSelector and ShutdownRegistrationStrategy.Log4jContextFactory(ShutdownCallbackRegistry shutdownCallbackRegistry)Constructs a Log4jContextFactory using the ContextSelector fromConstants.LOG4J_CONTEXT_SELECTORand the provided ShutdownRegistrationStrategy.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CancellableaddShutdownCallback(java.lang.Runnable callback)Adds a Runnable shutdown callback to this class.private static ContextSelectorcreateContextSelector()private static ShutdownCallbackRegistrycreateShutdownCallbackRegistry()LoggerContextgetContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.lang.Object externalContext, boolean currentContext)Loads the LoggerContext using the ContextSelector.LoggerContextgetContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.lang.Object externalContext, boolean currentContext, java.net.URI configLocation, java.lang.String name)Loads the LoggerContext using the ContextSelector.LoggerContextgetContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.lang.Object externalContext, boolean currentContext, java.util.List<java.net.URI> configLocations, java.lang.String name)LoggerContextgetContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.lang.Object externalContext, boolean currentContext, Configuration configuration)Loads the LoggerContext using the ContextSelector using the provided ConfigurationLoggerContextgetContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.lang.Object externalContext, boolean currentContext, ConfigurationSource source)Loads the LoggerContext using the ContextSelector.LoggerContextgetContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.util.Map.Entry<java.lang.String,java.lang.Object> entry, boolean currentContext, java.net.URI configLocation, java.lang.String name)ContextSelectorgetSelector()Returns the ContextSelector.ShutdownCallbackRegistrygetShutdownCallbackRegistry()Returns the ShutdownCallbackRegistrybooleanhasContext(java.lang.String fqcn, java.lang.ClassLoader loader, boolean currentContext)Checks to see if a LoggerContext is installed.private voidinitializeShutdownCallbackRegistry()booleanisClassLoaderDependent()Determines whether or not this factory and perhaps the underlying ContextSelector behavior depend on the callers classloader.booleanisShutdownHookEnabled()voidremoveContext(LoggerContext context)Removes knowledge of a LoggerContext.voidshutdown(java.lang.String fqcn, java.lang.ClassLoader loader, boolean currentContext, boolean allContexts)Shuts down the LoggerContext.
-
-
-
Field Detail
-
LOGGER
private static final StatusLogger LOGGER
-
SHUTDOWN_HOOK_ENABLED
private static final boolean SHUTDOWN_HOOK_ENABLED
-
selector
private final ContextSelector selector
-
shutdownCallbackRegistry
private final ShutdownCallbackRegistry shutdownCallbackRegistry
-
-
Constructor Detail
-
Log4jContextFactory
public Log4jContextFactory()
Initializes the ContextSelector from system propertyConstants.LOG4J_CONTEXT_SELECTOR.
-
Log4jContextFactory
public Log4jContextFactory(ContextSelector selector)
Initializes this factory's ContextSelector with the specified selector.- Parameters:
selector- the selector to use
-
Log4jContextFactory
public Log4jContextFactory(ShutdownCallbackRegistry shutdownCallbackRegistry)
Constructs a Log4jContextFactory using the ContextSelector fromConstants.LOG4J_CONTEXT_SELECTORand the provided ShutdownRegistrationStrategy.- Parameters:
shutdownCallbackRegistry- the ShutdownRegistrationStrategy to use- Since:
- 2.1
-
Log4jContextFactory
public Log4jContextFactory(ContextSelector selector, ShutdownCallbackRegistry shutdownCallbackRegistry)
Constructs a Log4jContextFactory using the provided ContextSelector and ShutdownRegistrationStrategy.- Parameters:
selector- the selector to useshutdownCallbackRegistry- the ShutdownRegistrationStrategy to use- Since:
- 2.1
-
-
Method Detail
-
createContextSelector
private static ContextSelector createContextSelector()
-
createShutdownCallbackRegistry
private static ShutdownCallbackRegistry createShutdownCallbackRegistry()
-
initializeShutdownCallbackRegistry
private void initializeShutdownCallbackRegistry()
-
getContext
public LoggerContext getContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.lang.Object externalContext, boolean currentContext)
Loads the LoggerContext using the ContextSelector.- Specified by:
getContextin interfaceLoggerContextFactory- Parameters:
fqcn- The fully qualified class name of the caller.loader- The ClassLoader to use or null.currentContext- If true returns the current Context, if false returns the Context appropriate for the caller if a more appropriate Context can be determined.externalContext- An external context (such as a ServletContext) to be associated with the LoggerContext.- Returns:
- The LoggerContext.
-
getContext
public LoggerContext getContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.lang.Object externalContext, boolean currentContext, ConfigurationSource source)
Loads the LoggerContext using the ContextSelector.- Parameters:
fqcn- The fully qualified class name of the caller.loader- The ClassLoader to use or null.externalContext- An external context (such as a ServletContext) to be associated with the LoggerContext.currentContext- If true returns the current Context, if false returns the Context appropriate for the caller if a more appropriate Context can be determined.source- The configuration source.- Returns:
- The LoggerContext.
-
getContext
public LoggerContext getContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.lang.Object externalContext, boolean currentContext, Configuration configuration)
Loads the LoggerContext using the ContextSelector using the provided Configuration- Parameters:
fqcn- The fully qualified class name of the caller.loader- The ClassLoader to use or null.externalContext- An external context (such as a ServletContext) to be associated with the LoggerContext.currentContext- If true returns the current Context, if false returns the Context appropriate for the caller if a more appropriate Context can be determined.configuration- The Configuration.- Returns:
- The LoggerContext.
-
getContext
public LoggerContext getContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.lang.Object externalContext, boolean currentContext, java.net.URI configLocation, java.lang.String name)
Loads the LoggerContext using the ContextSelector.- Specified by:
getContextin interfaceLoggerContextFactory- Parameters:
fqcn- The fully qualified class name of the caller.loader- The ClassLoader to use or null.externalContext- An external context (such as a ServletContext) to be associated with the LoggerContext.currentContext- If true returns the current Context, if false returns the Context appropriate for the caller if a more appropriate Context can be determined.configLocation- The location of the configuration for the LoggerContext (or null).name- The name of the context or null.- Returns:
- The LoggerContext.
-
getContext
public LoggerContext getContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.util.Map.Entry<java.lang.String,java.lang.Object> entry, boolean currentContext, java.net.URI configLocation, java.lang.String name)
-
getContext
public LoggerContext getContext(java.lang.String fqcn, java.lang.ClassLoader loader, java.lang.Object externalContext, boolean currentContext, java.util.List<java.net.URI> configLocations, java.lang.String name)
-
shutdown
public void shutdown(java.lang.String fqcn, java.lang.ClassLoader loader, boolean currentContext, boolean allContexts)Description copied from interface:LoggerContextFactoryShuts down the LoggerContext.- Specified by:
shutdownin interfaceLoggerContextFactory- Parameters:
fqcn- The fully qualified class name of the caller.loader- The ClassLoader to use or null.currentContext- If true shuts down the current Context, if false shuts down the Context appropriate for the caller if a more appropriate Context can be determined.allContexts- if true all LoggerContexts that can be located will be shutdown.
-
hasContext
public boolean hasContext(java.lang.String fqcn, java.lang.ClassLoader loader, boolean currentContext)Checks to see if a LoggerContext is installed.- Specified by:
hasContextin interfaceLoggerContextFactory- Parameters:
fqcn- The fully qualified class name of the caller.loader- The ClassLoader to use or null.currentContext- If true returns the current Context, if false returns the Context appropriate for the caller if a more appropriate Context can be determined.- Returns:
- true if a LoggerContext has been installed, false otherwise.
- Since:
- 3.0
-
getSelector
public ContextSelector getSelector()
Returns the ContextSelector.- Returns:
- The ContextSelector.
-
getShutdownCallbackRegistry
public ShutdownCallbackRegistry getShutdownCallbackRegistry()
Returns the ShutdownCallbackRegistry- Returns:
- the ShutdownCallbackRegistry
- Since:
- 2.4
-
removeContext
public void removeContext(LoggerContext context)
Removes knowledge of a LoggerContext.- Specified by:
removeContextin interfaceLoggerContextFactory- Parameters:
context- The context to remove.
-
isClassLoaderDependent
public boolean isClassLoaderDependent()
Description copied from interface:LoggerContextFactoryDetermines whether or not this factory and perhaps the underlying ContextSelector behavior depend on the callers classloader. This method should be overridden by implementations, however a default method is provided which always returnstrueto preserve the old behavior.- Specified by:
isClassLoaderDependentin interfaceLoggerContextFactory
-
addShutdownCallback
public Cancellable addShutdownCallback(java.lang.Runnable callback)
Description copied from interface:ShutdownCallbackRegistryAdds a Runnable shutdown callback to this class. Note: The returnedCancellablemust be retained on heap by caller to avoid premature garbage-collection of the registered callback (and to ensure the callback runs on shutdown).- Specified by:
addShutdownCallbackin interfaceShutdownCallbackRegistry- Parameters:
callback- the shutdown callback to be executed upon shutdown.- Returns:
- a Cancellable wrapper of the provided callback or
nullif the shutdown hook is disabled and cannot be added.
-
isShutdownHookEnabled
public boolean isShutdownHookEnabled()
-
-