Class WebLoggerContextUtils


  • public final class WebLoggerContextUtils
    extends java.lang.Object
    Convenience methods for retrieving the LoggerContext associated with a particular ServletContext. These methods are most particularly useful for asynchronous servlets where the Thread Context ClassLoader (TCCL) is potentially different from the TCCL used by the Servlet container that bootstrapped Log4j.
    Since:
    2.0.1
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static LoggerContext getRequiredWebLoggerContext​(javax.servlet.ServletContext servletContext)
      Finds the main LoggerContext configured for the given ServletContext.
      static javax.servlet.ServletContext getServletContext()
      Gets the current ServletContext if it has already been assigned to a LoggerContext's external context.
      static org.apache.logging.log4j.web.Log4jWebLifeCycle getWebLifeCycle​(javax.servlet.ServletContext servletContext)
      Finds or initializes the Log4jWebLifeCycle singleton for the given ServletContext.
      static LoggerContext getWebLoggerContext​(javax.servlet.ServletContext servletContext)
      Finds the main LoggerContext configured for the given ServletContext.
      static java.lang.Runnable wrapExecutionContext​(javax.servlet.ServletContext servletContext, java.lang.Runnable runnable)
      Wraps a Runnable instance by setting its thread context LoggerContext before execution and clearing it after execution.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getWebLoggerContext

        public static LoggerContext getWebLoggerContext​(javax.servlet.ServletContext servletContext)
        Finds the main LoggerContext configured for the given ServletContext.
        Parameters:
        servletContext - the ServletContext to locate a LoggerContext for
        Returns:
        the LoggerContext for the given ServletContext
        Since:
        2.0.1
      • getRequiredWebLoggerContext

        public static LoggerContext getRequiredWebLoggerContext​(javax.servlet.ServletContext servletContext)
        Finds the main LoggerContext configured for the given ServletContext.
        Parameters:
        servletContext - the ServletContext to locate a LoggerContext for
        Returns:
        the LoggerContext for the given ServletContext or null if none was set
        Throws:
        java.lang.IllegalStateException - if no LoggerContext could be found on the given ServletContext
        Since:
        2.0.1
      • getWebLifeCycle

        public static org.apache.logging.log4j.web.Log4jWebLifeCycle getWebLifeCycle​(javax.servlet.ServletContext servletContext)
        Finds or initializes the Log4jWebLifeCycle singleton for the given ServletContext.
        Parameters:
        servletContext - the ServletContext to get the Log4jWebLifeCycle for
        Returns:
        the Log4jWebLifeCycle for the given ServletContext
        Since:
        2.0.1
      • wrapExecutionContext

        public static java.lang.Runnable wrapExecutionContext​(javax.servlet.ServletContext servletContext,
                                                              java.lang.Runnable runnable)
        Wraps a Runnable instance by setting its thread context LoggerContext before execution and clearing it after execution.
        Parameters:
        servletContext - the ServletContext to locate a LoggerContext for
        runnable - the Runnable to wrap execution for
        Returns:
        a wrapped Runnable
        Since:
        2.0.1
      • getServletContext

        public static javax.servlet.ServletContext getServletContext()
        Gets the current ServletContext if it has already been assigned to a LoggerContext's external context.
        Returns:
        the current ServletContext attached to a LoggerContext or null if none could be found
        Since:
        2.1