/** * Gets the current {@link ServletContext} if it has already been assigned to a LoggerContext's external context. * * @return the current ServletContext attached to a LoggerContext or {@code null} if none could be found * @since 2.1 */ public static ServletContext getServletContext() { org.apache.logging.log4j.spi.LoggerContext lc = ContextAnchor.THREAD_CONTEXT.get(); if (lc == null) { lc = LogManager.getContext(false); } return lc == null ? null : lc.getExternalContext() instanceof ServletContext ? (ServletContext) lc.getExternalContext() : null; } }