001/* 002 * Copyright 2002-2012 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.context.request; 018 019import org.apache.log4j.Logger; 020import org.apache.log4j.NDC; 021 022import org.springframework.ui.ModelMap; 023 024/** 025 * Request logging interceptor that adds a request context message to the 026 * Log4J nested diagnostic context (NDC) before the request is processed, 027 * removing it again after the request is processed. 028 * 029 * @author Juergen Hoeller 030 * @since 2.5 031 * @see org.apache.log4j.NDC#push(String) 032 * @see org.apache.log4j.NDC#pop() 033 * @deprecated as of Spring 4.2.1, in favor of Apache Log4j 2 034 * (following Apache's EOL declaration for log4j 1.x) 035 */ 036@Deprecated 037public class Log4jNestedDiagnosticContextInterceptor implements AsyncWebRequestInterceptor { 038 039 /** Logger available to subclasses */ 040 protected final Logger log4jLogger = Logger.getLogger(getClass()); 041 042 private boolean includeClientInfo = false; 043 044 045 /** 046 * Set whether or not the session id and user name should be included 047 * in the log message. 048 */ 049 public void setIncludeClientInfo(boolean includeClientInfo) { 050 this.includeClientInfo = includeClientInfo; 051 } 052 053 /** 054 * Return whether or not the session id and user name should be included 055 * in the log message. 056 */ 057 protected boolean isIncludeClientInfo() { 058 return this.includeClientInfo; 059 } 060 061 062 /** 063 * Adds a message the Log4J NDC before the request is processed. 064 */ 065 @Override 066 public void preHandle(WebRequest request) throws Exception { 067 NDC.push(getNestedDiagnosticContextMessage(request)); 068 } 069 070 /** 071 * Determine the message to be pushed onto the Log4J nested diagnostic context. 072 * <p>Default is the request object's {@code getDescription} result. 073 * @param request current HTTP request 074 * @return the message to be pushed onto the Log4J NDC 075 * @see WebRequest#getDescription 076 * @see #isIncludeClientInfo() 077 */ 078 protected String getNestedDiagnosticContextMessage(WebRequest request) { 079 return request.getDescription(isIncludeClientInfo()); 080 } 081 082 @Override 083 public void postHandle(WebRequest request, ModelMap model) throws Exception { 084 } 085 086 /** 087 * Removes the log message from the Log4J NDC after the request is processed. 088 */ 089 @Override 090 public void afterCompletion(WebRequest request, Exception ex) throws Exception { 091 NDC.pop(); 092 if (NDC.getDepth() == 0) { 093 NDC.remove(); 094 } 095 } 096 097 /** 098 * Removes the log message from the Log4J NDC when the processing thread is 099 * exited after the start of asynchronous request handling. 100 */ 101 @Override 102 public void afterConcurrentHandlingStarted(WebRequest request) { 103 NDC.pop(); 104 if (NDC.getDepth() == 0) { 105 NDC.remove(); 106 } 107 } 108 109}