001/* 002 * Copyright 2002-2012 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; 020 021/** 022 * AOP Alliance {@code MethodInterceptor} that can be introduced in a chain 023 * to display verbose information about intercepted invocations to the logger. 024 * 025 * <p>Logs full invocation details on method entry and method exit, 026 * including invocation arguments and invocation count. This is only 027 * intended for debugging purposes; use {@code SimpleTraceInterceptor} 028 * or {@code CustomizableTraceInterceptor} for pure tracing purposes. 029 * 030 * @author Rod Johnson 031 * @author Juergen Hoeller 032 * @see SimpleTraceInterceptor 033 * @see CustomizableTraceInterceptor 034 */ 035@SuppressWarnings("serial") 036public class DebugInterceptor extends SimpleTraceInterceptor { 037 038 private volatile long count; 039 040 041 /** 042 * Create a new DebugInterceptor with a static logger. 043 */ 044 public DebugInterceptor() { 045 } 046 047 /** 048 * Create a new DebugInterceptor with dynamic or static logger, 049 * according to the given flag. 050 * @param useDynamicLogger whether to use a dynamic logger or a static logger 051 * @see #setUseDynamicLogger 052 */ 053 public DebugInterceptor(boolean useDynamicLogger) { 054 setUseDynamicLogger(useDynamicLogger); 055 } 056 057 058 @Override 059 public Object invoke(MethodInvocation invocation) throws Throwable { 060 synchronized (this) { 061 this.count++; 062 } 063 return super.invoke(invocation); 064 } 065 066 @Override 067 protected String getInvocationDescription(MethodInvocation invocation) { 068 return invocation + "; count=" + this.count; 069 } 070 071 072 /** 073 * Return the number of times this interceptor has been invoked. 074 */ 075 public long getCount() { 076 return this.count; 077 } 078 079 /** 080 * Reset the invocation count to zero. 081 */ 082 public synchronized void resetCount() { 083 this.count = 0; 084 } 085 086}