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 javax.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 javax.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 javax.servlet.ServletContext} parameter name for the location of the configuration.
43       */
44      String LOG4J_CONFIG_LOCATION = "log4jConfiguration";
45  
46      /**
47       * The {@link javax.servlet.ServletContext} parameter name for the JNDI flag.
48       */
49      String IS_LOG4J_CONTEXT_SELECTOR_NAMED = "isLog4jContextSelectorNamed";
50  
51      /**
52       * The {@link javax.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 attribute key for the {@link javax.servlet.ServletContext} attribute that the singleton support instance
60       * is stored in.
61       */
62      String SUPPORT_ATTRIBUTE = Log4jWebSupport.class.getName() + ".INSTANCE";
63  
64      /**
65       * The attribute key for the {@link javax.servlet.ServletContext} attribute that the {@link LoggerContext}
66       * is stored in.
67       */
68      String CONTEXT_ATTRIBUTE = LoggerContext.class.getName() + ".INSTANCE";
69  
70      /**
71       * Sets the logger context so that code executing afterwards can easily and quickly access loggers via
72       * {@link org.apache.logging.log4j.LogManager#getLogger}.
73       */
74      void setLoggerContext();
75  
76      /**
77       * Clears the logger context set up in {@link #setLoggerContext}.
78       */
79      void clearLoggerContext();
80  
81      /**
82       * Sets the logger context by calling {@link #setLoggerContext}, executes the runnable argument, then clears the
83       * logger context by calling {@link #clearLoggerContext}.
84       *
85       * @param runnable The runnable to execute wrapped with a configured logger context
86       */
87      void wrapExecution(Runnable runnable);
88  }