public class CustomizableTraceInterceptor extends AbstractTraceInterceptor
MethodInterceptor
implementation that allows for highly customizable
method-level tracing, using placeholders.
Trace messages are written on method entry, and if the method invocation succeeds on method exit. If an invocation results in an exception, then an exception message is written. The contents of these trace messages is fully customizable and special placeholders are available to allow you to include runtime information in your log messages. The placeholders available are:
$[methodName]
- replaced with the name of the method being invoked$[targetClassName]
- replaced with the name of the class that is
the target of the invocation$[targetClassShortName]
- replaced with the short name of the class
that is the target of the invocation$[returnValue]
- replaced with the value returned by the invocation$[argumentTypes]
- replaced with a comma-separated list of the
short class names of the method arguments$[arguments]
- replaced with a comma-separated list of the
String
representation of the method arguments$[exception]
- replaced with the String
representation
of any Throwable
raised during the invocation$[invocationTime]
- replaced with the time, in milliseconds,
taken by the method invocationThere are restrictions on which placeholders can be used in which messages: see the individual message properties for details on the valid placeholders.
setEnterMessage(java.lang.String)
,
setExitMessage(java.lang.String)
,
setExceptionMessage(java.lang.String)
,
SimpleTraceInterceptor
,
Serialized FormModifier and Type | Field and Description |
---|---|
static java.lang.String |
PLACEHOLDER_ARGUMENT_TYPES
The
$[argumentTypes] placeholder. |
static java.lang.String |
PLACEHOLDER_ARGUMENTS
The
$[arguments] placeholder. |
static java.lang.String |
PLACEHOLDER_EXCEPTION
The
$[exception] placeholder. |
static java.lang.String |
PLACEHOLDER_INVOCATION_TIME
The
$[invocationTime] placeholder. |
static java.lang.String |
PLACEHOLDER_METHOD_NAME
The
$[methodName] placeholder. |
static java.lang.String |
PLACEHOLDER_RETURN_VALUE
The
$[returnValue] placeholder. |
static java.lang.String |
PLACEHOLDER_TARGET_CLASS_NAME
The
$[targetClassName] placeholder. |
static java.lang.String |
PLACEHOLDER_TARGET_CLASS_SHORT_NAME
The
$[targetClassShortName] placeholder. |
defaultLogger
Constructor and Description |
---|
CustomizableTraceInterceptor() |
Modifier and Type | Method and Description |
---|---|
protected java.lang.Object |
invokeUnderTrace(MethodInvocation invocation,
Log logger)
Writes a log message before the invocation based on the value of
enterMessage . |
protected java.lang.String |
replacePlaceholders(java.lang.String message,
MethodInvocation methodInvocation,
java.lang.Object returnValue,
java.lang.Throwable throwable,
long invocationTime)
Replace the placeholders in the given message with the supplied values,
or values derived from those supplied.
|
void |
setEnterMessage(java.lang.String enterMessage)
Set the template used for method entry log messages.
|
void |
setExceptionMessage(java.lang.String exceptionMessage)
Set the template used for method exception log messages.
|
void |
setExitMessage(java.lang.String exitMessage)
Set the template used for method exit log messages.
|
getClassForLogging, getLoggerForInvocation, invoke, isInterceptorEnabled, isLogEnabled, setHideProxyClassNames, setLogExceptionStackTrace, setLoggerName, setUseDynamicLogger, writeToLog, writeToLog
public static final java.lang.String PLACEHOLDER_METHOD_NAME
$[methodName]
placeholder.
Replaced with the name of the method being invoked.public static final java.lang.String PLACEHOLDER_TARGET_CLASS_NAME
$[targetClassName]
placeholder.
Replaced with the fully-qualified name of the Class
of the method invocation target.public static final java.lang.String PLACEHOLDER_TARGET_CLASS_SHORT_NAME
$[targetClassShortName]
placeholder.
Replaced with the short name of the Class
of the
method invocation target.public static final java.lang.String PLACEHOLDER_RETURN_VALUE
$[returnValue]
placeholder.
Replaced with the String
representation of the value
returned by the method invocation.public static final java.lang.String PLACEHOLDER_ARGUMENT_TYPES
$[argumentTypes]
placeholder.
Replaced with a comma-separated list of the argument types for the
method invocation. Argument types are written as short class names.public static final java.lang.String PLACEHOLDER_ARGUMENTS
$[arguments]
placeholder.
Replaced with a comma separated list of the argument values for the
method invocation. Relies on the toString()
method of
each argument type.public static final java.lang.String PLACEHOLDER_EXCEPTION
$[exception]
placeholder.
Replaced with the String
representation of any
Throwable
raised during method invocation.public static final java.lang.String PLACEHOLDER_INVOCATION_TIME
$[invocationTime]
placeholder.
Replaced with the time taken by the invocation (in milliseconds).public void setEnterMessage(java.lang.String enterMessage) throws java.lang.IllegalArgumentException
$[targetClassName]
$[targetClassShortName]
$[argumentTypes]
$[arguments]
java.lang.IllegalArgumentException
public void setExitMessage(java.lang.String exitMessage)
$[targetClassName]
$[targetClassShortName]
$[argumentTypes]
$[arguments]
$[returnValue]
$[invocationTime]
public void setExceptionMessage(java.lang.String exceptionMessage)
$[targetClassName]
$[targetClassShortName]
$[argumentTypes]
$[arguments]
$[exception]
protected java.lang.Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws java.lang.Throwable
enterMessage
.
If the invocation succeeds, then a log message is written on exit based on the value
exitMessage
. If an exception occurs during invocation, then a message is
written based on the value of exceptionMessage
.invokeUnderTrace
in class AbstractTraceInterceptor
logger
- the Log
to write trace messages toMethodInvocation.proceed()
java.lang.Throwable
- if the call to MethodInvocation.proceed()
encountered any errorssetEnterMessage(java.lang.String)
,
setExitMessage(java.lang.String)
,
setExceptionMessage(java.lang.String)
protected java.lang.String replacePlaceholders(java.lang.String message, MethodInvocation methodInvocation, @Nullable java.lang.Object returnValue, @Nullable java.lang.Throwable throwable, long invocationTime)
message
- the message template containing the placeholders to be replacedmethodInvocation
- the MethodInvocation
being logged.
Used to derive values for all placeholders except $[exception]
and $[returnValue]
.returnValue
- any value returned by the invocation.
Used to replace the $[returnValue]
placeholder. May be null
.throwable
- any Throwable
raised during the invocation.
The value of Throwable.toString()
is replaced for the
$[exception]
placeholder. May be null
.invocationTime
- the value to write in place of the
$[invocationTime]
placeholder