Class AbstractTraceInterceptor
- All Implemented Interfaces:
Serializable
,Advice
,Interceptor
,MethodInterceptor
- Direct Known Subclasses:
AbstractMonitoringInterceptor
,CustomizableTraceInterceptor
,SimpleTraceInterceptor
MethodInterceptor
implementation for tracing.
By default, log messages are written to the log for the interceptor class,
not the class which is being intercepted. Setting the useDynamicLogger
bean property to true
causes all log messages to be written to
the Log
for the target class being intercepted.
Subclasses must implement the invokeUnderTrace
method, which
is invoked by this class ONLY when a particular invocation SHOULD be traced.
Subclasses should write to the Log
instance provided.
- Since:
- 1.2
- Author:
- Rob Harrop, Juergen Hoeller
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected Log
The defaultLog
instance used to write trace messages. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected Class<?>
getClassForLogging
(Object target) Determine the class to use for logging purposes.protected Log
getLoggerForInvocation
(MethodInvocation invocation) Return the appropriateLog
instance to use for the givenMethodInvocation
.invoke
(MethodInvocation invocation) Determines whether or not logging is enabled for the particularMethodInvocation
.protected abstract Object
invokeUnderTrace
(MethodInvocation invocation, Log logger) Subclasses must override this method to perform any tracing around the suppliedMethodInvocation
.protected boolean
isInterceptorEnabled
(MethodInvocation invocation, Log logger) Determine whether the interceptor should kick in, that is, whether theinvokeUnderTrace
method should be called.protected boolean
isLogEnabled
(Log logger) Determine whether the givenLog
instance is enabled.void
setHideProxyClassNames
(boolean hideProxyClassNames) Set to "true" to havedynamic loggers
hide proxy class names wherever possible.void
setLogExceptionStackTrace
(boolean logExceptionStackTrace) Set whether to pass an exception to the logger, suggesting inclusion of its stack trace into the log.void
setLoggerName
(String loggerName) Set the name of the logger to use.void
setUseDynamicLogger
(boolean useDynamicLogger) Set whether to use a dynamic logger or a static logger.protected void
writeToLog
(Log logger, String message) Write the supplied trace message to the suppliedLog
instance.protected void
writeToLog
(Log logger, String message, Throwable ex) Write the supplied trace message andThrowable
to the suppliedLog
instance.
-
Field Details
-
defaultLogger
The defaultLog
instance used to write trace messages. This instance is mapped to the implementingClass
.
-
-
Constructor Details
-
AbstractTraceInterceptor
public AbstractTraceInterceptor()
-
-
Method Details
-
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
Log
instance should be used to write log messages for a particular method invocation: a dynamic one for theClass
getting called, or a static one for theClass
of the trace interceptor.NOTE: Specify either this property or "loggerName", not both.
-
setLoggerName
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 loggers
hide 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).- Since:
- 4.3.10
-
invoke
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 theinvokeUnderTrace
method for handling.- Specified by:
invoke
in interfaceMethodInterceptor
- Parameters:
invocation
- the method invocation joinpoint- Returns:
- the result of the call to
Joinpoint.proceed()
; might be intercepted by the interceptor - Throws:
Throwable
- if the interceptors or the target object throws an exception- See Also:
-
getLoggerForInvocation
Return the appropriateLog
instance to use for the givenMethodInvocation
. If theuseDynamicLogger
flag is set, theLog
instance will be for the target class of theMethodInvocation
, otherwise theLog
will be the default static logger.- Parameters:
invocation
- theMethodInvocation
being traced- Returns:
- the
Log
instance to use - See Also:
-
getClassForLogging
Determine the class to use for logging purposes.- Parameters:
target
- the target object to introspect- Returns:
- the target class for the given object
- See Also:
-
isInterceptorEnabled
Determine whether the interceptor should kick in, that is, whether theinvokeUnderTrace
method should be called.Default behavior is to check whether the given
Log
instance is enabled. Subclasses can override this to apply the interceptor in other cases as well.- Parameters:
invocation
- theMethodInvocation
being tracedlogger
- theLog
instance to check- See Also:
-
isLogEnabled
Determine whether the givenLog
instance is enabled.Default is
true
when the "trace" level is enabled. Subclasses can override this to change the level under which 'tracing' occurs.- Parameters:
logger
- theLog
instance to check
-
writeToLog
Write the supplied trace message to the suppliedLog
instance.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.- Since:
- 4.3.10
- See Also:
-
writeToLog
Write the supplied trace message andThrowable
to the suppliedLog
instance.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
TRACE
level. Subclasses can override this method to control which level the message is written at, typically also overridingisLogEnabled(org.apache.commons.logging.Log)
accordingly.- Since:
- 4.3.10
- See Also:
-
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 theMethodInvocation
actually executes by callingMethodInvocation.proceed()
.By default, the passed-in
Log
instance will have log level "trace" enabled. Subclasses do not have to check for this again, unless they overwrite theisInterceptorEnabled
method to modify the default behavior, and may delegate towriteToLog
for actual messages to be written.- Parameters:
logger
- theLog
to write trace messages to- Returns:
- the result of the call to
MethodInvocation.proceed()
- Throws:
Throwable
- if the call toMethodInvocation.proceed()
encountered any errors- See Also:
-