alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (Log4jNestedDiagnosticContextInterceptor.java)

This example Spring Framework source code file (Log4jNestedDiagnosticContextInterceptor.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

exception, exception, log4jnesteddiagnosticcontextinterceptor, log4jnesteddiagnosticcontextinterceptor, logger, modelmap, modelmap, string, webrequestinterceptor, webrequestinterceptor

The Spring Framework Log4jNestedDiagnosticContextInterceptor.java source code

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

package org.springframework.web.context.request;

import org.apache.log4j.Logger;
import org.apache.log4j.NDC;

import org.springframework.ui.ModelMap;

/**
 * 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.
 *
 * @author Juergen Hoeller
 * @since 2.5
 * @see org.apache.log4j.NDC#push(String)
 * @see org.apache.log4j.NDC#pop()
 */
public class Log4jNestedDiagnosticContextInterceptor implements WebRequestInterceptor {

	/** Logger available to subclasses */
	protected final Logger log4jLogger = Logger.getLogger(getClass());

	private boolean includeClientInfo = false;


	/**
	 * Set whether or not the session id and user name should be included
	 * in the log message.
	 */
	public void setIncludeClientInfo(boolean includeClientInfo) {
		this.includeClientInfo = includeClientInfo;
	}

	/**
	 * Return whether or not the session id and user name should be included
	 * in the log message.
	 */
	protected boolean isIncludeClientInfo() {
		return this.includeClientInfo;
	}


	/**
	 * Adds a message the Log4J NDC before the request is processed.
	 */
	public void preHandle(WebRequest request) throws Exception {
		NDC.push(getNestedDiagnosticContextMessage(request));
	}

	/**
	 * Determine the message to be pushed onto the Log4J nested diagnostic context.
	 * <p>Default is the request object's getDescription result.
	 * @param request current HTTP request
	 * @return the message to be pushed onto the Log4J NDC
	 * @see WebRequest#getDescription
	 * @see #isIncludeClientInfo()
	 */
	protected String getNestedDiagnosticContextMessage(WebRequest request) {
		return request.getDescription(isIncludeClientInfo());
	}

	public void postHandle(WebRequest request, ModelMap model) throws Exception {
	}

	/**
	 * Removes the log message from the Log4J NDC after the request is processed.
	 */
	public void afterCompletion(WebRequest request, Exception ex) throws Exception {
		NDC.pop();
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework Log4jNestedDiagnosticContextInterceptor.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.