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.core.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}.<br>
26   * <br>
27   * You can obtain the instance of this for your web application by retrieving the {@link javax.servlet.ServletContext}
28   * attribute named {@code org.apache.logging.log4j.core.web.Log4jWebSupport.INSTANCE}. If needed, you can also obtain
29   * the {@link LoggerContext} instance for your web application by retrieving the {@code ServletContext} attribute named
30   * {@code org.apache.logging.log4j.spi.LoggerContext.INSTANCE}.
31   */
32  public interface Log4jWebSupport {
33      /**
34       * The {@link javax.servlet.ServletContext} parameter name for the name of the
35       * {@link org.apache.logging.log4j.core.LoggerContext}.
36       */
37      String LOG4J_CONTEXT_NAME = "log4jContextName";
38  
39      /**
40       * The {@link javax.servlet.ServletContext} parameter name for the location of the configuration.
41       */
42      String LOG4J_CONFIG_LOCATION = "log4jConfiguration";
43  
44      /**
45       * The {@link javax.servlet.ServletContext} parameter name for the JNDI flag.
46       */
47      String IS_LOG4J_CONTEXT_SELECTOR_NAMED = "isLog4jContextSelectorNamed";
48  
49      /**
50       * The {@link javax.servlet.ServletContext} parameter name for the flag that disables Log4j's auto-initialization
51       * in Servlet 3.0+ web applications. Set a context parameter with this name to "true" to disable
52       * auto-initialization.
53       */
54      String IS_LOG4J_AUTO_INITIALIZATION_DISABLED = "isLog4jAutoInitializationDisabled";
55  
56      /**
57       * The attribute key for the {@link javax.servlet.ServletContext} attribute that the singleton support instance
58       * is stored in.
59       */
60      String SUPPORT_ATTRIBUTE = Log4jWebSupport.class.getName() + ".INSTANCE";
61  
62      /**
63       * The attribute key for the {@link javax.servlet.ServletContext} attribute that the {@link LoggerContext}
64       * is stored in.
65       */
66      String CONTEXT_ATTRIBUTE = LoggerContext.class.getName() + ".INSTANCE";
67  
68      /**
69       * Sets the logger context so that code executing afterwards can easily and quickly access loggers via
70       * {@link org.apache.logging.log4j.LogManager#getLogger}.
71       */
72      void setLoggerContext();
73  
74      /**
75       * Clears the logger context set up in {@link #setLoggerContext}.
76       */
77      void clearLoggerContext();
78  
79      /**
80       * Sets the logger context by calling {@link #setLoggerContext}, executes the runnable argument, then clears the
81       * logger context by calling {@link #clearLoggerContext}.
82       *
83       * @param runnable The runnable to execute wrapped with a configured logger context
84       */
85      void wrapExecution(Runnable runnable);
86  }