Top Description Fields Constructors Methods
org.springframework.aop.interceptor

public abstract Class AbstractTraceInterceptor

extends Object
implements MethodInterceptor, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, org.aopalliance.intercept.MethodInterceptor, org.aopalliance.intercept.Interceptor, org.aopalliance.aop.Advice
Known Direct Subclasses
org.springframework.aop.interceptor.CustomizableTraceInterceptor, org.springframework.aop.interceptor.SimpleTraceInterceptor, org.springframework.aop.interceptor.AbstractMonitoringInterceptor
Annotations
@SuppressWarnings:serial
Imports
java.io.Serializable, org.aopalliance.intercept.MethodInterceptor, .MethodInvocation, org.apache.commons.logging.Log, .LogFactory, org.springframework.aop.support.AopUtils, org.springframework.lang.Nullable, org.springframework.util.Assert

Base 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.

Authors
Rob Harrop, Juergen Hoeller
Since
1.2
See Also
setUseDynamicLogger, invokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log)

Field Summary

Modifier and TypeField and Description
protected transient Log
defaultLogger

The default Log instance used to write trace messages.

private boolean
hideProxyClassNames

Indicates whether proxy class names should be hidden when using dynamic loggers.

private boolean
logExceptionStackTrace

Indicates whether to pass an exception to the logger.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected Class<?>

Returns:

the target class for the given object
getClassForLogging
(Object
the target object to introspect
target
)

Determine the class to use for logging purposes.

protected Log

Returns:

the Log instance to use
getLoggerForInvocation
(MethodInvocation
the MethodInvocation being traced
invocation
)

Return the appropriate Log instance to use for the given MethodInvocation.

public Object
invoke(MethodInvocation
the method invocation joinpoint
invocation
)

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Determines whether logging is enabled for the particular MethodInvocation.

protected abstract Object

Returns:

the result of the call to MethodInvocation.proceed()
invokeUnderTrace
(MethodInvocation invocation, Log
the Log to write trace messages to
logger
)

Subclasses must override this method to perform any tracing around the supplied MethodInvocation.

protected boolean
isInterceptorEnabled(MethodInvocation
the MethodInvocation being traced
invocation
,
Log
the Log instance to check
logger
)

Determine whether the interceptor should kick in, that is, whether the invokeUnderTrace method should be called.

protected boolean
isLogEnabled(Log
the Log instance to check
logger
)

Determine whether the given Log instance is enabled.

public void
setHideProxyClassNames(boolean hideProxyClassNames)

Set to "true" to have dynamic loggers hide proxy class names wherever possible.

public void
setLogExceptionStackTrace(boolean logExceptionStackTrace)

Set whether to pass an exception to the logger, suggesting inclusion of its stack trace into the log.

public void
setLoggerName(String loggerName)

Set the name of the logger to use.

public 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 supplied Log instance.

protected void
writeToLog(Log logger, String message, Throwable ex)

Write the supplied trace message and Throwable to the supplied Log instance.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

defaultLoggerback to summary
protected transient Log defaultLogger

The default Log instance used to write trace messages. This instance is mapped to the implementing Class.

Annotations
@Nullable
hideProxyClassNamesback to summary
private boolean hideProxyClassNames

Indicates whether proxy class names should be hidden when using dynamic loggers.

See Also
setUseDynamicLogger
logExceptionStackTraceback to summary
private boolean logExceptionStackTrace

Indicates whether to pass an exception to the logger.

See Also
writeToLog(Log, String, Throwable)

Constructor Detail

AbstractTraceInterceptorback to summary
public AbstractTraceInterceptor()

Method Detail

getClassForLoggingback to summary
protected Class<?> getClassForLogging(Object target)

Determine the class to use for logging purposes.

Parameters
target:Object

the target object to introspect

Returns:Class<?>

the target class for the given object

See Also
setHideProxyClassNames
getLoggerForInvocationback to summary
protected Log getLoggerForInvocation(MethodInvocation invocation)

Return the appropriate 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.

Parameters
invocation:MethodInvocation

the MethodInvocation being traced

Returns:Log

the Log instance to use

See Also
setUseDynamicLogger
invokeback to summary
public Object invoke(MethodInvocation invocation) throws Throwable

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Determines whether logging is enabled for the particular MethodInvocation. If not, the method invocation proceeds as normal, otherwise the method invocation is passed to the invokeUnderTrace method for handling.

Parameters
invocation:MethodInvocation

Doc from org.aopalliance.intercept.MethodInterceptor.invoke.

the method invocation joinpoint

Returns:Object

Doc from org.aopalliance.intercept.MethodInterceptor.invoke.

the result of the call to Joinpoint#proceed(); might be intercepted by the interceptor

Annotations
@Override
@Nullable
Exceptions
Throwable:

Doc from org.aopalliance.intercept.MethodInterceptor.invoke.

if the interceptors or the target object throws an exception

See Also
invokeUnderTrace(org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log)
invokeUnderTraceback to summary
protected abstract Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable

Subclasses must override this method to perform any tracing around the supplied 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.

Parameters
logger:Log

the Log to write trace messages to

Returns:Object

the result of the call to MethodInvocation.proceed()

Annotations
@Nullable
Exceptions
Throwable:
if the call to MethodInvocation.proceed() encountered any errors
See Also
isLogEnabled, writeToLog(Log, String), writeToLog(Log, String, Throwable)
isInterceptorEnabledback to summary
protected boolean isInterceptorEnabled(MethodInvocation invocation, Log logger)

Determine whether the interceptor should kick in, that is, whether the 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.

Parameters
invocation:MethodInvocation

the MethodInvocation being traced

logger:Log

the Log instance to check

See Also
invokeUnderTrace, isLogEnabled
isLogEnabledback to summary
protected boolean isLogEnabled(Log logger)

Determine whether the given 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.

Parameters
logger:Log

the Log instance to check

setHideProxyClassNamesback to summary
public void setHideProxyClassNames(boolean hideProxyClassNames)

Set to "true" to have dynamic loggers hide proxy class names wherever possible. Default is "false".

setLogExceptionStackTraceback to summary
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
setLoggerNameback to summary
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.

See Also
org.apache.commons.logging.LogFactory#getLog(String), java.util.logging.Logger#getLogger(String)
setUseDynamicLoggerback to summary
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 the Class getting called, or a static one for the Class of the trace interceptor.

Note

Specify either this property or "loggerName", not both.

See Also
getLoggerForInvocation(org.aopalliance.intercept.MethodInvocation)
writeToLogback to summary
protected void writeToLog(Log logger, String message)

Write the supplied trace message to the supplied Log instance.

To be called by invokeUnderTrace 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(Log, String, Throwable)
writeToLogback to summary
protected void writeToLog(Log logger, String message, Throwable ex)

Write the supplied trace message and Throwable to the supplied Log instance.

To be called by invokeUnderTrace for enter/exit outcomes, potentially including an exception. Note that an exception's stack trace won't get logged when setLogExceptionStackTrace 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 accordingly.

Since
4.3.10
See Also
setLogExceptionStackTrace, isLogEnabled