View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.web;
18  
19  import org.apache.logging.log4j.spi.LoggerContext;
20  
21  /**
22   * Specifies an interface for setting and clearing a thread-bound {@link LoggerContext} in a Java EE web application.
23   * Also defines constants for context parameter and attribute names. In most cases you will never need to use this
24   * directly because the Log4j filter handles this task automatically. However, in async operations you should wrap
25   * code that executes in separate threads with {@link #setLoggerContext} and {@link #clearLoggerContext}.
26   *
27   * <p>
28   * You can obtain the instance of this for your web application by retrieving the {@link jakarta.servlet.ServletContext}
29   * attribute named {@code org.apache.logging.log4j.core.web.Log4jWebSupport.INSTANCE}. If needed, you can also obtain
30   * the {@link LoggerContext} instance for your web application by retrieving the {@code ServletContext} attribute named
31   * {@code org.apache.logging.log4j.spi.LoggerContext.INSTANCE}.
32   * </p>
33   */
34  public interface Log4jWebSupport {
35      /**
36       * The {@link jakarta.servlet.ServletContext} parameter name for the name of the
37       * {@link org.apache.logging.log4j.core.LoggerContext}.
38       */
39      String LOG4J_CONTEXT_NAME = "log4jContextName";
40  
41      /**
42       * The {@link jakarta.servlet.ServletContext} parameter name for the location of the configuration.
43       */
44      String LOG4J_CONFIG_LOCATION = "log4jConfiguration";
45  
46      /**
47       * The {@link jakarta.servlet.ServletContext} parameter name for the JNDI flag.
48       */
49      String IS_LOG4J_CONTEXT_SELECTOR_NAMED = "isLog4jContextSelectorNamed";
50  
51      /**
52       * The {@link jakarta.servlet.ServletContext} parameter name for the flag that disables Log4j's auto-initialization
53       * in Servlet 3.0+ web applications. Set a context parameter with this name to "true" to disable
54       * auto-initialization.
55       */
56      String IS_LOG4J_AUTO_INITIALIZATION_DISABLED = "isLog4jAutoInitializationDisabled";
57  
58      /**
59       * The {@link jakarta.servlet.ServletContext} parameter name for the flag that disables Log4j's auto-shutdown
60       * in Servlet 3.0+ web applications. Set a context parameter with this name to "true" to disable
61       * auto-shutdown.
62       */
63      String IS_LOG4J_AUTO_SHUTDOWN_DISABLED = "isLog4jAutoShutdownDisabled";
64  
65      /**
66       * The attribute key for the {@link jakarta.servlet.ServletContext} attribute that the singleton support instance
67       * is stored in.
68       */
69      String SUPPORT_ATTRIBUTE = Log4jWebSupport.class.getName() + ".INSTANCE";
70  
71      /**
72       * The attribute key for the {@link jakarta.servlet.ServletContext} attribute that the {@link LoggerContext}
73       * is stored in.
74       */
75      String CONTEXT_ATTRIBUTE = LoggerContext.class.getName() + ".INSTANCE";
76  
77      /**
78       * Sets the logger context so that code executing afterwards can easily and quickly access loggers via
79       * {@link org.apache.logging.log4j.LogManager#getLogger}.
80       */
81      void setLoggerContext();
82  
83      /**
84       * Clears the logger context set up in {@link #setLoggerContext}.
85       */
86      void clearLoggerContext();
87  
88      /**
89       * Sets the logger context by calling {@link #setLoggerContext}, executes the runnable argument, then clears the
90       * logger context by calling {@link #clearLoggerContext}.
91       *
92       * @param runnable The runnable to execute wrapped with a configured logger context
93       */
94      void wrapExecution(Runnable runnable);
95  }