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.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}. 026 * 027 * <p> 028 * You can obtain the instance of this for your web application by retrieving the {@link javax.servlet.ServletContext} 029 * attribute named {@code org.apache.logging.log4j.core.web.Log4jWebSupport.INSTANCE}. If needed, you can also obtain 030 * the {@link LoggerContext} instance for your web application by retrieving the {@code ServletContext} attribute named 031 * {@code org.apache.logging.log4j.spi.LoggerContext.INSTANCE}. 032 * </p> 033 */ 034public interface Log4jWebSupport { 035 /** 036 * The {@link javax.servlet.ServletContext} parameter name for the name of the 037 * {@link org.apache.logging.log4j.core.LoggerContext}. 038 */ 039 String LOG4J_CONTEXT_NAME = "log4jContextName"; 040 041 /** 042 * The {@link javax.servlet.ServletContext} parameter name for the location of the configuration. 043 */ 044 String LOG4J_CONFIG_LOCATION = "log4jConfiguration"; 045 046 /** 047 * The {@link javax.servlet.ServletContext} parameter name for the JNDI flag. 048 */ 049 String IS_LOG4J_CONTEXT_SELECTOR_NAMED = "isLog4jContextSelectorNamed"; 050 051 /** 052 * The {@link javax.servlet.ServletContext} parameter name for the flag that disables Log4j's auto-initialization 053 * in Servlet 3.0+ web applications. Set a context parameter with this name to "true" to disable 054 * auto-initialization. 055 */ 056 String IS_LOG4J_AUTO_INITIALIZATION_DISABLED = "isLog4jAutoInitializationDisabled"; 057 058 /** 059 * The {@link javax.servlet.ServletContext} parameter name for the flag that disables Log4j's auto-shutdown 060 * in Servlet 3.0+ web applications. Set a context parameter with this name to "true" to disable 061 * auto-shutdown. 062 */ 063 String IS_LOG4J_AUTO_SHUTDOWN_DISABLED = "isLog4jAutoShutdownDisabled"; 064 065 /** 066 * The attribute key for the {@link javax.servlet.ServletContext} attribute that the singleton support instance 067 * is stored in. 068 */ 069 String SUPPORT_ATTRIBUTE = Log4jWebSupport.class.getName() + ".INSTANCE"; 070 071 /** 072 * The attribute key for the {@link javax.servlet.ServletContext} attribute that the {@link LoggerContext} 073 * is stored in. 074 */ 075 String CONTEXT_ATTRIBUTE = LoggerContext.class.getName() + ".INSTANCE"; 076 077 /** 078 * Sets the logger context so that code executing afterwards can easily and quickly access loggers via 079 * {@link org.apache.logging.log4j.LogManager#getLogger}. 080 */ 081 void setLoggerContext(); 082 083 /** 084 * Clears the logger context set up in {@link #setLoggerContext}. 085 */ 086 void clearLoggerContext(); 087 088 /** 089 * Sets the logger context by calling {@link #setLoggerContext}, executes the runnable argument, then clears the 090 * logger context by calling {@link #clearLoggerContext}. 091 * 092 * @param runnable The runnable to execute wrapped with a configured logger context 093 */ 094 void wrapExecution(Runnable runnable); 095}