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 */ 017 package org.apache.logging.log4j.web; 018 019 import java.io.IOException; 020 021 import javax.servlet.Filter; 022 import javax.servlet.FilterChain; 023 import javax.servlet.FilterConfig; 024 import javax.servlet.ServletContext; 025 import javax.servlet.ServletException; 026 import javax.servlet.ServletRequest; 027 import javax.servlet.ServletResponse; 028 029 /** 030 * This is responsible for the following: 031 * <ul> 032 * <li>Clearing the logger context when the application has finished starting up.</li> 033 * <li>Setting the logger context before processing a request and clearing it after processing a request.</li> 034 * <li>Setting the logger context when the application is starting to shut down.</li> 035 * </ul> 036 * This filter is a once-per-request filter. It is capable of filtering all the different types of requests 037 * (standard, asynchronous, error, etc.) but will not apply processing if the filter matches multiple times on the same 038 * logical request. 039 */ 040 public class Log4jServletFilter implements Filter { 041 042 static final String ALREADY_FILTERED_ATTRIBUTE = Log4jServletFilter.class.getName() + ".FILTERED"; 043 044 private ServletContext servletContext; 045 private Log4jWebLifeCycle initializer; 046 047 @Override 048 public void init(final FilterConfig filterConfig) throws ServletException { 049 this.servletContext = filterConfig.getServletContext(); 050 this.servletContext.log("Log4jServletFilter initialized."); 051 052 this.initializer = Log4jWebInitializerImpl.getLog4jWebInitializer(this.servletContext); 053 this.initializer.clearLoggerContext(); // the application is mostly finished starting up now 054 } 055 056 @Override 057 public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) 058 throws IOException, ServletException { 059 if (request.getAttribute(ALREADY_FILTERED_ATTRIBUTE) != null) { 060 chain.doFilter(request, response); 061 } else { 062 request.setAttribute(ALREADY_FILTERED_ATTRIBUTE, true); 063 064 try { 065 this.initializer.setLoggerContext(); 066 067 chain.doFilter(request, response); 068 } finally { 069 this.initializer.clearLoggerContext(); 070 } 071 } 072 } 073 074 @Override 075 public void destroy() { 076 if (this.servletContext == null || this.initializer == null) { 077 throw new IllegalStateException("Filter destroyed before it was initialized."); 078 } 079 this.servletContext.log("Log4jServletFilter destroyed."); 080 081 this.initializer.setLoggerContext(); // the application is just now starting to shut down 082 } 083 }