Spring Framework

org.springframework.web.context.request
Class Log4jNestedDiagnosticContextInterceptor

java.lang.Object
  extended by org.springframework.web.context.request.Log4jNestedDiagnosticContextInterceptor
All Implemented Interfaces:
WebRequestInterceptor

public class Log4jNestedDiagnosticContextInterceptor
extends java.lang.Object
implements WebRequestInterceptor

Request logging interceptor that adds a request context message to the Log4J nested diagnostic context (NDC) before the request is processed, removing it again after the request is processed.

Since:
2.5
Author:
Juergen Hoeller
See Also:
NDC.push(String), NDC.pop()

Field Summary
protected  org.apache.log4j.Logger log4jLogger
          Logger available to subclasses
 
Constructor Summary
Log4jNestedDiagnosticContextInterceptor()
           
 
Method Summary
 void afterCompletion(WebRequest request, java.lang.Exception ex)
          Removes the log message from the Log4J NDC after the request is processed.
protected  java.lang.String getNestedDiagnosticContextMessage(WebRequest request)
          Determine the message to be pushed onto the Log4J nested diagnostic context.
protected  boolean isIncludeClientInfo()
          Return whether or not the session id and user name should be included in the log message.
 void postHandle(WebRequest request, ModelMap model)
          Intercept the execution of a request handler after its successful invocation, right before view rendering (if any).
 void preHandle(WebRequest request)
          Adds a message the Log4J NDC before the request is processed.
 void setIncludeClientInfo(boolean includeClientInfo)
          Set whether or not the session id and user name should be included in the log message.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log4jLogger

protected final org.apache.log4j.Logger log4jLogger
Logger available to subclasses

Constructor Detail

Log4jNestedDiagnosticContextInterceptor

public Log4jNestedDiagnosticContextInterceptor()
Method Detail

setIncludeClientInfo

public void setIncludeClientInfo(boolean includeClientInfo)
Set whether or not the session id and user name should be included in the log message.


isIncludeClientInfo

protected boolean isIncludeClientInfo()
Return whether or not the session id and user name should be included in the log message.


preHandle

public void preHandle(WebRequest request)
               throws java.lang.Exception
Adds a message the Log4J NDC before the request is processed.

Specified by:
preHandle in interface WebRequestInterceptor
Parameters:
request - the current web request
Throws:
java.lang.Exception - in case of errors

getNestedDiagnosticContextMessage

protected java.lang.String getNestedDiagnosticContextMessage(WebRequest request)
Determine the message to be pushed onto the Log4J nested diagnostic context.

Default is the request object's getDescription result.

Parameters:
request - current HTTP request
Returns:
the message to be pushed onto the Log4J NDC
See Also:
WebRequest.getDescription(boolean), isIncludeClientInfo()

postHandle

public void postHandle(WebRequest request,
                       ModelMap model)
                throws java.lang.Exception
Description copied from interface: WebRequestInterceptor
Intercept the execution of a request handler after its successful invocation, right before view rendering (if any).

Allows for modifying context resources after successful handler execution (for example, flushing a Hibernate Session).

Specified by:
postHandle in interface WebRequestInterceptor
Parameters:
request - the current web request
model - the map of model objects that will be exposed to the view (may be null). Can be used to analyze the exposed model and/or to add further model attributes, if desired.
Throws:
java.lang.Exception - in case of errors

afterCompletion

public void afterCompletion(WebRequest request,
                            java.lang.Exception ex)
                     throws java.lang.Exception
Removes the log message from the Log4J NDC after the request is processed.

Specified by:
afterCompletion in interface WebRequestInterceptor
Parameters:
request - the current web request
ex - exception thrown on handler execution, if any
Throws:
java.lang.Exception - in case of errors

Spring Framework