org.springframework.web.context.request

## Class Log4jNestedDiagnosticContextInterceptor

- java.lang.Object
- - org.springframework.web.context.request.Log4jNestedDiagnosticContextInterceptor

All Implemented Interfaces:
  AsyncWebRequestInterceptor, WebRequestInterceptor

    public class Log4jNestedDiagnosticContextInterceptor
    extends Object
    implements AsyncWebRequestInterceptor

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

Fields
`protected Logger` `log4jLogger` Logger available to subclasses

### Constructor Summary

Constructors
Constructor and Description
`Log4jNestedDiagnosticContextInterceptor()`

### Method Summary

Methods
`void` `afterCompletion(WebRequest request, Exception ex)` Removes the log message from the Log4J NDC after the request is processed.
`void` `afterConcurrentHandlingStarted(WebRequest request)` Removes the log message from the Log4J NDC when the processing thread is exited after the start of asynchronous request handling.
`protected 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 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 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:
  `Exception` - in case of errors

#### getNestedDiagnosticContextMessage

    protected 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 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:
  `Exception` - in case of errors

#### afterCompletion

    public void afterCompletion(WebRequest request,
                       Exception ex)
                         throws 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:
  `Exception` - in case of errors

#### afterConcurrentHandlingStarted

    public void afterConcurrentHandlingStarted(WebRequest request)

Removes the log message from the Log4J NDC when the processing thread is exited after the start of asynchronous request handling.

Specified by:
  `afterConcurrentHandlingStarted` in interface `AsyncWebRequestInterceptor`
Parameters:
  `request` - the current request