Class JndiContextSelector

java.lang.Object
org.apache.logging.log4j.core.selector.JndiContextSelector
All Implemented Interfaces:
ContextSelector, NamedContextSelector

public class JndiContextSelector extends Object implements NamedContextSelector
This class can be used to define a custom logger repository. It makes use of the fact that in J2EE environments, each web-application is guaranteed to have its own JNDI context relative to the java:comp/env context. In EJBs, each enterprise bean (albeit not each application) has its own context relative to the java:comp/env context. An env-entry in a deployment descriptor provides the information to the JNDI context. Once the env-entry is set, a repository selector can query the JNDI application context to look up the value of the entry. The logging context of the web-application will depend on the value the env-entry. The JNDI context which is looked up by this class is java:comp/env/log4j/context-name.

For security reasons, JNDI must be enabled by setting system property log4j2.enableJndiContextSelector=true.

Here is an example of an env-entry:

 <env-entry>
   <description>JNDI logging context name for this app</description>
   <env-entry-name>log4j/context-name</env-entry-name>
   <env-entry-value>aDistinctiveLoggingContextName</env-entry-value>
   <env-entry-type>java.lang.String</env-entry-type>
 </env-entry>
 

If multiple applications use the same logging context name, then they will share the same logging context.

You can also specify the URL for this context's configuration resource. This repository selector (ContextJNDISelector) will use this resource to automatically configure the log4j repository.

 <env-entry>
   <description>URL for configuring log4j context</description>
   <env-entry-name>log4j/configuration-resource</env-entry-name>
   <env-entry-value>urlOfConfigurationResource</env-entry-value>
   <env-entry-type>java.lang.String</env-entry-type>
 </env-entry>
 

It usually good practice for configuration resources of distinct applications to have distinct names. However, if this is not possible Naming

  • Constructor Details

    • JndiContextSelector

      public JndiContextSelector()
  • Method Details

    • shutdown

      public void shutdown(String fqcn, ClassLoader loader, boolean currentContext, boolean allContexts)
      Description copied from interface: ContextSelector
      Shuts down the LoggerContext.
      Specified by:
      shutdown in interface ContextSelector
      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
      allContexts - if true all LoggerContexts that can be located will be shutdown.
    • hasContext

      public boolean hasContext(String fqcn, ClassLoader loader, boolean currentContext)
      Description copied from interface: ContextSelector
      Checks to see if a LoggerContext is installed. The default implementation returns false.
      Specified by:
      hasContext in interface ContextSelector
      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.
    • getContext

      public LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext)
      Description copied from interface: ContextSelector
      Returns the LoggerContext.
      Specified by:
      getContext in interface ContextSelector
      Parameters:
      fqcn - The fully qualified class name of the caller.
      loader - 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:
      The LoggerContext.
    • getContext

      public LoggerContext getContext(String fqcn, ClassLoader loader, boolean currentContext, URI configLocation)
      Description copied from interface: ContextSelector
      Returns the LoggerContext.
      Specified by:
      getContext in interface ContextSelector
      Parameters:
      fqcn - The fully qualified class name of the caller.
      loader - 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.
      configLocation - The location of the configuration for the LoggerContext.
      Returns:
      The LoggerContext.
    • locateContext

      public LoggerContext locateContext(String name, Object externalContext, URI configLocation)
      Description copied from interface: NamedContextSelector
      Locate the LoggerContext with the specified name.
      Specified by:
      locateContext in interface NamedContextSelector
      Parameters:
      name - The LoggerContext name.
      externalContext - The external context to associate with the LoggerContext.
      configLocation - The location of the configuration.
      Returns:
      A LoggerContext.
    • removeContext

      public void removeContext(LoggerContext context)
      Description copied from interface: ContextSelector
      Remove any references to the LoggerContext.
      Specified by:
      removeContext in interface ContextSelector
      Parameters:
      context - The context to remove.
    • isClassLoaderDependent

      public boolean isClassLoaderDependent()
      Description copied from interface: ContextSelector
      Determines whether or not this ContextSelector depends on the callers classloader. This method should be overridden by implementations, however a default method is provided which always returns true to preserve the old behavior.
      Specified by:
      isClassLoaderDependent in interface ContextSelector
      Returns:
      true if the class loader is required.
    • removeContext

      public LoggerContext removeContext(String name)
      Description copied from interface: NamedContextSelector
      Locate the LoggerContext with the specified name using the default configuration.
      Specified by:
      removeContext in interface NamedContextSelector
      Parameters:
      name - The LoggerContext name.
      Returns:
      A LoggerContext.
    • getLoggerContexts

      public List<LoggerContext> getLoggerContexts()
      Description copied from interface: ContextSelector
      Returns a List of all the available LoggerContexts.
      Specified by:
      getLoggerContexts in interface ContextSelector
      Returns:
      The List of LoggerContexts.