类 AbstractTraceInterceptor
- java.lang.Object
- org.springframework.aop.interceptor.AbstractTraceInterceptor
- 所有已实现的接口:
Serializable,Advice,Interceptor,MethodInterceptor
public abstract class AbstractTraceInterceptor extends Object implements MethodInterceptor, Serializable
BaseMethodInterceptorimplementation for tracing.By default, log messages are written to the log for the interceptor class, not the class which is being intercepted. Setting the
useDynamicLoggerbean property totruecauses all log messages to be written to theLogfor the target class being intercepted.Subclasses must implement the
invokeUnderTracemethod, which is invoked by this class ONLY when a particular invocation SHOULD be traced. Subclasses should write to theLoginstance provided.- 从以下版本开始:
- 1.2
- 作者:
- Rob Harrop, Juergen Hoeller
- 另请参阅:
setUseDynamicLogger(boolean),invokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log), 序列化表格
字段概要
字段 修饰符和类型 字段 说明 protected LogdefaultLoggerThe defaultLoginstance used to write trace messages.
构造器概要
构造器 构造器 说明 AbstractTraceInterceptor()
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 protected Class<?>getClassForLogging(Object target)Determine the class to use for logging purposes.protected LoggetLoggerForInvocation(MethodInvocation invocation)Return the appropriateLoginstance to use for the givenMethodInvocation.Objectinvoke(MethodInvocation invocation)Determines whether or not logging is enabled for the particularMethodInvocation.protected abstract ObjectinvokeUnderTrace(MethodInvocation invocation, Log logger)Subclasses must override this method to perform any tracing around the suppliedMethodInvocation.protected booleanisInterceptorEnabled(MethodInvocation invocation, Log logger)Determine whether the interceptor should kick in, that is, whether theinvokeUnderTracemethod should be called.protected booleanisLogEnabled(Log logger)Determine whether the givenLoginstance is enabled.voidsetHideProxyClassNames(boolean hideProxyClassNames)Set to "true" to havedynamic loggershide proxy class names wherever possible.voidsetLogExceptionStackTrace(boolean logExceptionStackTrace)Set whether to pass an exception to the logger, suggesting inclusion of its stack trace into the log.voidsetLoggerName(String loggerName)Set the name of the logger to use.voidsetUseDynamicLogger(boolean useDynamicLogger)Set whether to use a dynamic logger or a static logger.protected voidwriteToLog(Log logger, String message)Write the supplied trace message to the suppliedLoginstance.protected voidwriteToLog(Log logger, String message, Throwable ex)Write the supplied trace message andThrowableto the suppliedLoginstance.
字段详细资料
defaultLogger
@Nullable protected transient Log defaultLogger
The defaultLoginstance used to write trace messages. This instance is mapped to the implementingClass.
构造器详细资料
AbstractTraceInterceptor
public AbstractTraceInterceptor()
方法详细资料
setUseDynamicLogger
public void setUseDynamicLogger(boolean useDynamicLogger)
Set whether to use a dynamic logger or a static logger. Default is a static logger for this trace interceptor.Used to determine which
Loginstance should be used to write log messages for a particular method invocation: a dynamic one for theClassgetting called, or a static one for theClassof the trace interceptor.NOTE: Specify either this property or "loggerName", not both.
setLoggerName
public void setLoggerName(String loggerName)
Set the name of the logger to use. The name will be passed to the underlying logger implementation through Commons Logging, getting interpreted as log category according to the logger's configuration.This can be specified to not log into the category of a class (whether this interceptor's class or the class getting called) but rather into a specific named category.
NOTE: Specify either this property or "useDynamicLogger", not both.
setHideProxyClassNames
public void setHideProxyClassNames(boolean hideProxyClassNames)
Set to "true" to havedynamic loggershide proxy class names wherever possible. Default is "false".
setLogExceptionStackTrace
public void setLogExceptionStackTrace(boolean logExceptionStackTrace)
Set whether to pass an exception to the logger, suggesting inclusion of its stack trace into the log. Default is "true"; set this to "false" in order to reduce the log output to just the trace message (which may include the exception class name and exception message, if applicable).- 从以下版本开始:
- 4.3.10
invoke
@Nullable public Object invoke(MethodInvocation invocation) throws Throwable
Determines whether or not logging is enabled for the particularMethodInvocation. If not, the method invocation proceeds as normal, otherwise the method invocation is passed to theinvokeUnderTracemethod for handling.- 指定者:
invoke在接口中MethodInterceptor- 参数:
invocation- the method invocation joinpoint- 返回:
- the result of the call to
Joinpoint.proceed(); might be intercepted by the interceptor - 抛出:
Throwable- if the interceptors or the target object throws an exception- 另请参阅:
invokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log)
getLoggerForInvocation
protected Log getLoggerForInvocation(MethodInvocation invocation)
Return the appropriateLoginstance to use for the givenMethodInvocation. If theuseDynamicLoggerflag is set, theLoginstance will be for the target class of theMethodInvocation, otherwise theLogwill be the default static logger.- 参数:
invocation- theMethodInvocationbeing traced- 返回:
- the
Loginstance to use - 另请参阅:
setUseDynamicLogger(boolean)
getClassForLogging
protected Class<?> getClassForLogging(Object target)
Determine the class to use for logging purposes.- 参数:
target- the target object to introspect- 返回:
- the target class for the given object
- 另请参阅:
setHideProxyClassNames(boolean)
isInterceptorEnabled
protected boolean isInterceptorEnabled(MethodInvocation invocation, Log logger)
Determine whether the interceptor should kick in, that is, whether theinvokeUnderTracemethod should be called.Default behavior is to check whether the given
Loginstance is enabled. Subclasses can override this to apply the interceptor in other cases as well.- 参数:
invocation- theMethodInvocationbeing tracedlogger- theLoginstance to check- 另请参阅:
invokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log),isLogEnabled(org.apache.commons.logging.Log)
isLogEnabled
protected boolean isLogEnabled(Log logger)
Determine whether the givenLoginstance is enabled.Default is
truewhen the "trace" level is enabled. Subclasses can override this to change the level under which 'tracing' occurs.- 参数:
logger- theLoginstance to check
writeToLog
protected void writeToLog(Log logger, String message)
Write the supplied trace message to the suppliedLoginstance.To be called by
invokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log)for enter/exit messages.Delegates to
writeToLog(Log, String, Throwable)as the ultimate delegate that controls the underlying logger invocation.- 从以下版本开始:
- 4.3.10
- 另请参阅:
writeToLog(Log, String, Throwable)
writeToLog
protected void writeToLog(Log logger, String message, @Nullable Throwable ex)
Write the supplied trace message andThrowableto the suppliedLoginstance.To be called by
invokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log)for enter/exit outcomes, potentially including an exception. Note that an exception's stack trace won't get logged whensetLogExceptionStackTrace(boolean)is "false".By default messages are written at
TRACElevel. Subclasses can override this method to control which level the message is written at, typically also overridingisLogEnabled(org.apache.commons.logging.Log)accordingly.- 从以下版本开始:
- 4.3.10
- 另请参阅:
setLogExceptionStackTrace(boolean),isLogEnabled(org.apache.commons.logging.Log)
invokeUnderTrace
@Nullable protected abstract Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable
Subclasses must override this method to perform any tracing around the suppliedMethodInvocation. Subclasses are responsible for ensuring that theMethodInvocationactually executes by callingMethodInvocation.proceed().By default, the passed-in
Loginstance will have log level "trace" enabled. Subclasses do not have to check for this again, unless they overwrite theisInterceptorEnabledmethod to modify the default behavior, and may delegate towriteToLogfor actual messages to be written.- 参数:
logger- theLogto write trace messages to- 返回:
- the result of the call to
MethodInvocation.proceed() - 抛出:
Throwable- if the call toMethodInvocation.proceed()encountered any errors- 另请参阅:
isLogEnabled(org.apache.commons.logging.Log),writeToLog(Log, String),writeToLog(Log, String, Throwable)