public abstract class AbstractTraceInterceptor extends java.lang.Object implements MethodInterceptor, java.io.Serializable
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.
setUseDynamicLogger(boolean)
,
invokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log)
,
Serialized FormModifier and Type | Field and Description |
---|---|
protected Log |
defaultLogger
The default
Log instance used to write trace messages. |
private boolean |
hideProxyClassNames
Indicates whether or not proxy class names should be hidden when using dynamic loggers.
|
private boolean |
logExceptionStackTrace
Indicates whether to pass an exception to the logger.
|
Constructor and Description |
---|
AbstractTraceInterceptor() |
Modifier and Type | Method and Description |
---|---|
protected java.lang.Class<?> |
getClassForLogging(java.lang.Object target)
Determine the class to use for logging purposes.
|
protected Log |
getLoggerForInvocation(MethodInvocation invocation)
Return the appropriate
Log instance to use for the given
MethodInvocation . |
java.lang.Object |
invoke(MethodInvocation invocation)
Determines whether or not logging is enabled for the particular
MethodInvocation . |
protected abstract java.lang.Object |
invokeUnderTrace(MethodInvocation invocation,
Log logger)
Subclasses must override this method to perform any tracing around the
supplied
MethodInvocation . |
protected boolean |
isInterceptorEnabled(MethodInvocation invocation,
Log logger)
Determine whether the interceptor should kick in, that is,
whether the
invokeUnderTrace method should be called. |
protected boolean |
isLogEnabled(Log logger)
Determine whether the given
Log instance is enabled. |
void |
setHideProxyClassNames(boolean hideProxyClassNames)
Set to "true" to have
dynamic 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(java.lang.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,
java.lang.String message)
Write the supplied trace message to the supplied
Log instance. |
protected void |
writeToLog(Log logger,
java.lang.String message,
java.lang.Throwable ex)
Write the supplied trace message and
Throwable to the
supplied Log instance. |
@Nullable protected transient Log defaultLogger
Log
instance used to write trace messages.
This instance is mapped to the implementing Class
.private boolean hideProxyClassNames
setUseDynamicLogger(boolean)
private boolean logExceptionStackTrace
writeToLog(Log, String, Throwable)
public void setUseDynamicLogger(boolean useDynamicLogger)
Used to determine which Log
instance should be used to write
log messages for a particular method invocation: a dynamic one for the
Class
getting called, or a static one for the Class
of the trace interceptor.
NOTE: Specify either this property or "loggerName", not both.
public void setLoggerName(java.lang.String loggerName)
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.
LogFactory.getLog(String)
,
Logger.getLogger(String)
public void setHideProxyClassNames(boolean hideProxyClassNames)
dynamic loggers
hide
proxy class names wherever possible. Default is "false".public void setLogExceptionStackTrace(boolean logExceptionStackTrace)
@Nullable public java.lang.Object invoke(MethodInvocation invocation) throws java.lang.Throwable
MethodInvocation
.
If not, the method invocation proceeds as normal, otherwise the method invocation is passed
to the invokeUnderTrace
method for handling.invoke
in interface MethodInterceptor
invocation
- the method invocation joinpointJoinpoint.proceed()
;
might be intercepted by the interceptorjava.lang.Throwable
- if the interceptors or the target object
throws an exceptioninvokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log)
protected Log getLoggerForInvocation(MethodInvocation invocation)
Log
instance to use for the given
MethodInvocation
. If the useDynamicLogger
flag
is set, the Log
instance will be for the target class of the
MethodInvocation
, otherwise the Log
will be the
default static logger.invocation
- the MethodInvocation
being tracedLog
instance to usesetUseDynamicLogger(boolean)
protected java.lang.Class<?> getClassForLogging(java.lang.Object target)
target
- the target object to introspectsetHideProxyClassNames(boolean)
protected boolean isInterceptorEnabled(MethodInvocation invocation, Log logger)
invokeUnderTrace
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.
invocation
- the MethodInvocation
being tracedlogger
- the Log
instance to checkinvokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log)
,
isLogEnabled(org.apache.commons.logging.Log)
protected boolean isLogEnabled(Log logger)
Log
instance is enabled.
Default is true
when the "trace" level is enabled.
Subclasses can override this to change the level under which 'tracing' occurs.
logger
- the Log
instance to checkprotected void writeToLog(Log logger, java.lang.String message)
Log
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.
writeToLog(Log, String, Throwable)
protected void writeToLog(Log logger, java.lang.String message, @Nullable java.lang.Throwable ex)
Throwable
to the
supplied Log
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 when setLogExceptionStackTrace(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 overriding isLogEnabled(org.apache.commons.logging.Log)
accordingly.
setLogExceptionStackTrace(boolean)
,
isLogEnabled(org.apache.commons.logging.Log)
@Nullable protected abstract java.lang.Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws java.lang.Throwable
MethodInvocation
. Subclasses are responsible for
ensuring that the MethodInvocation
actually executes by
calling MethodInvocation.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 the isInterceptorEnabled
method to modify
the default behavior, and may delegate to writeToLog
for actual
messages to be written.
logger
- the Log
to write trace messages toMethodInvocation.proceed()
java.lang.Throwable
- if the call to MethodInvocation.proceed()
encountered any errorsisLogEnabled(org.apache.commons.logging.Log)
,
writeToLog(Log, String)
,
writeToLog(Log, String, Throwable)