KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > filter > Log4jNestedDiagnosticContextFilter


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.filter;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20
21 import org.apache.log4j.Logger;
22 import org.apache.log4j.NDC;
23
24 /**
25  * Request logging filter that adds the request log message to the Log4J
26  * nested diagnostic context (NDC) before the request is processed,
27  * removing it again after the request is processed.
28  *
29  * @author Rob Harrop
30  * @author Juergen Hoeller
31  * @since 1.2.5
32  * @see #setIncludeQueryString
33  * @see #setBeforeMessagePrefix
34  * @see #setBeforeMessageSuffix
35  * @see #setAfterMessagePrefix
36  * @see #setAfterMessageSuffix
37  * @see org.apache.log4j.NDC#push(String)
38  * @see org.apache.log4j.NDC#pop()
39  */

40 public class Log4jNestedDiagnosticContextFilter extends AbstractRequestLoggingFilter {
41
42     /** Logger available to subclasses */
43     protected final Logger log4jLogger = Logger.getLogger(getClass());
44
45
46     /**
47      * Logs the before-request message through Log4J and
48      * adds a message the Log4J NDC before the request is processed.
49      */

50     protected void beforeRequest(HttpServletRequest JavaDoc request, String JavaDoc message) {
51         if (log4jLogger.isDebugEnabled()) {
52             log4jLogger.debug(message);
53         }
54         NDC.push(getNestedDiagnosticContextMessage(request));
55     }
56
57     /**
58      * Determine the message to be pushed onto the Log4J nested diagnostic context.
59      * <p>Default is a plain request log message without prefix or suffix.
60      * @param request current HTTP request
61      * @return the message to be pushed onto the Log4J NDC
62      * @see #createMessage
63      */

64     protected String JavaDoc getNestedDiagnosticContextMessage(HttpServletRequest JavaDoc request) {
65         return createMessage(request, "", "");
66     }
67
68     /**
69      * Removes the log message from the Log4J NDC after the request is processed
70      * and logs the after-request message through Log4J.
71      */

72     protected void afterRequest(HttpServletRequest JavaDoc request, String JavaDoc message) {
73         NDC.pop();
74         if (log4jLogger.isDebugEnabled()) {
75             log4jLogger.debug(message);
76         }
77     }
78
79 }
80
Popular Tags