001/* 002 * Copyright 2002-2017 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.aop.interceptor; 018 019import org.aopalliance.intercept.MethodInvocation; 020import org.apache.commons.logging.Log; 021 022/** 023 * Simple AOP Alliance {@code MethodInterceptor} that can be introduced 024 * in a chain to display verbose trace information about intercepted method 025 * invocations, with method entry and method exit info. 026 * 027 * <p>Consider using {@code CustomizableTraceInterceptor} for more 028 * advanced needs. 029 * 030 * @author Dmitriy Kopylenko 031 * @author Juergen Hoeller 032 * @since 1.2 033 * @see CustomizableTraceInterceptor 034 */ 035@SuppressWarnings("serial") 036public class SimpleTraceInterceptor extends AbstractTraceInterceptor { 037 038 /** 039 * Create a new SimpleTraceInterceptor with a static logger. 040 */ 041 public SimpleTraceInterceptor() { 042 } 043 044 /** 045 * Create a new SimpleTraceInterceptor with dynamic or static logger, 046 * according to the given flag. 047 * @param useDynamicLogger whether to use a dynamic logger or a static logger 048 * @see #setUseDynamicLogger 049 */ 050 public SimpleTraceInterceptor(boolean useDynamicLogger) { 051 setUseDynamicLogger(useDynamicLogger); 052 } 053 054 055 @Override 056 protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable { 057 String invocationDescription = getInvocationDescription(invocation); 058 writeToLog(logger, "Entering " + invocationDescription); 059 try { 060 Object rval = invocation.proceed(); 061 writeToLog(logger, "Exiting " + invocationDescription); 062 return rval; 063 } 064 catch (Throwable ex) { 065 writeToLog(logger, "Exception thrown in " + invocationDescription, ex); 066 throw ex; 067 } 068 } 069 070 /** 071 * Return a description for the given method invocation. 072 * @param invocation the invocation to describe 073 * @return the description 074 */ 075 protected String getInvocationDescription(MethodInvocation invocation) { 076 return "method '" + invocation.getMethod().getName() + "' of class [" + 077 invocation.getThis().getClass().getName() + "]"; 078 } 079 080}