001/* 002 * Copyright 2002-2008 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.filter; 018 019import javax.servlet.http.HttpServletRequest; 020 021import org.apache.log4j.Logger; 022import org.apache.log4j.NDC; 023 024/** 025 * Request logging filter that adds the request log message to the Log4J 026 * nested diagnostic context (NDC) before the request is processed, 027 * removing it again after the request is processed. 028 * 029 * @author Juergen Hoeller 030 * @author Rob Harrop 031 * @since 1.2.5 032 * @see #setIncludeQueryString 033 * @see #setBeforeMessagePrefix 034 * @see #setBeforeMessageSuffix 035 * @see #setAfterMessagePrefix 036 * @see #setAfterMessageSuffix 037 * @see org.apache.log4j.NDC#push(String) 038 * @see org.apache.log4j.NDC#pop() 039 * @deprecated as of Spring 4.2.1, in favor of Apache Log4j 2 040 * (following Apache's EOL declaration for log4j 1.x) 041 */ 042@Deprecated 043public class Log4jNestedDiagnosticContextFilter extends AbstractRequestLoggingFilter { 044 045 /** Logger available to subclasses */ 046 protected final Logger log4jLogger = Logger.getLogger(getClass()); 047 048 049 /** 050 * Logs the before-request message through Log4J and 051 * adds a message the Log4J NDC before the request is processed. 052 */ 053 @Override 054 protected void beforeRequest(HttpServletRequest request, String message) { 055 if (log4jLogger.isDebugEnabled()) { 056 log4jLogger.debug(message); 057 } 058 NDC.push(getNestedDiagnosticContextMessage(request)); 059 } 060 061 /** 062 * Determine the message to be pushed onto the Log4J nested diagnostic context. 063 * <p>Default is a plain request log message without prefix or suffix. 064 * @param request current HTTP request 065 * @return the message to be pushed onto the Log4J NDC 066 * @see #createMessage 067 */ 068 protected String getNestedDiagnosticContextMessage(HttpServletRequest request) { 069 return createMessage(request, "", ""); 070 } 071 072 /** 073 * Removes the log message from the Log4J NDC after the request is processed 074 * and logs the after-request message through Log4J. 075 */ 076 @Override 077 protected void afterRequest(HttpServletRequest request, String message) { 078 NDC.pop(); 079 if (NDC.getDepth() == 0) { 080 NDC.remove(); 081 } 082 if (log4jLogger.isDebugEnabled()) { 083 log4jLogger.debug(message); 084 } 085 } 086 087}