001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.web;
018
019import org.apache.logging.log4j.spi.LoggerContext;
020
021/**
022 * Specifies an interface for setting and clearing a thread-bound {@link LoggerContext} in a Java EE web application.
023 * Also defines constants for context parameter and attribute names. In most cases you will never need to use this
024 * directly because the Log4j filter handles this task automatically. However, in async operations you should wrap
025 * code that executes in separate threads with {@link #setLoggerContext} and {@link #clearLoggerContext}.<br>
026 * <br>
027 * You can obtain the instance of this for your web application by retrieving the {@link javax.servlet.ServletContext}
028 * attribute named {@code org.apache.logging.log4j.core.web.Log4jWebSupport.INSTANCE}. If needed, you can also obtain
029 * the {@link LoggerContext} instance for your web application by retrieving the {@code ServletContext} attribute named
030 * {@code org.apache.logging.log4j.spi.LoggerContext.INSTANCE}.
031 */
032public interface Log4jWebSupport {
033    /**
034     * The {@link javax.servlet.ServletContext} parameter name for the name of the
035     * {@link org.apache.logging.log4j.core.LoggerContext}.
036     */
037    String LOG4J_CONTEXT_NAME = "log4jContextName";
038
039    /**
040     * The {@link javax.servlet.ServletContext} parameter name for the location of the configuration.
041     */
042    String LOG4J_CONFIG_LOCATION = "log4jConfiguration";
043
044    /**
045     * The {@link javax.servlet.ServletContext} parameter name for the JNDI flag.
046     */
047    String IS_LOG4J_CONTEXT_SELECTOR_NAMED = "isLog4jContextSelectorNamed";
048
049    /**
050     * The {@link javax.servlet.ServletContext} parameter name for the flag that disables Log4j's auto-initialization
051     * in Servlet 3.0+ web applications. Set a context parameter with this name to "true" to disable
052     * auto-initialization.
053     */
054    String IS_LOG4J_AUTO_INITIALIZATION_DISABLED = "isLog4jAutoInitializationDisabled";
055
056    /**
057     * The attribute key for the {@link javax.servlet.ServletContext} attribute that the singleton support instance
058     * is stored in.
059     */
060    String SUPPORT_ATTRIBUTE = Log4jWebSupport.class.getName() + ".INSTANCE";
061
062    /**
063     * The attribute key for the {@link javax.servlet.ServletContext} attribute that the {@link LoggerContext}
064     * is stored in.
065     */
066    String CONTEXT_ATTRIBUTE = LoggerContext.class.getName() + ".INSTANCE";
067
068    /**
069     * Sets the logger context so that code executing afterwards can easily and quickly access loggers via
070     * {@link org.apache.logging.log4j.LogManager#getLogger}.
071     */
072    void setLoggerContext();
073
074    /**
075     * Clears the logger context set up in {@link #setLoggerContext}.
076     */
077    void clearLoggerContext();
078
079    /**
080     * Sets the logger context by calling {@link #setLoggerContext}, executes the runnable argument, then clears the
081     * logger context by calling {@link #clearLoggerContext}.
082     *
083     * @param runnable The runnable to execute wrapped with a configured logger context
084     */
085    void wrapExecution(Runnable runnable);
086}