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

public Class CustomizableTraceInterceptor

extends AbstractTraceInterceptor
Class Inheritance
Annotations
@SuppressWarnings:serial
Imports
java.util.Set, java.util.regex.Matcher, .Pattern, org.aopalliance.intercept.MethodInvocation, org.apache.commons.logging.Log, org.springframework.lang.Nullable, org.springframework.util.Assert, .ClassUtils, .StopWatch, .StringUtils

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:

There are restrictions on which placeholders can be used in which messages: see the individual message properties for details on the valid placeholders.

Authors
Rob Harrop, Juergen Hoeller, Sam Brannen
Since
1.2
See Also
setEnterMessage, setExitMessage, setExceptionMessage, SimpleTraceInterceptor

Field Summary

Modifier and TypeField and Description
pack-priv static final Set<String>
ALLOWED_PLACEHOLDERS

The Set of allowed placeholders.

private static final String
DEFAULT_ENTER_MESSAGE

The default message used for writing method entry messages.

private static final String
DEFAULT_EXCEPTION_MESSAGE

The default message used for writing exception messages.

private static final String
DEFAULT_EXIT_MESSAGE

The default message used for writing method exit messages.

private String
enterMessage

The message for method entry.

private String
exceptionMessage

The message for exceptions during method execution.

private String
exitMessage

The message for method exit.

private static final Pattern
PATTERN

The Pattern used to match placeholders.

public static final String
PLACEHOLDER_ARGUMENT_TYPES

The $[argumentTypes] placeholder.

public static final String
PLACEHOLDER_ARGUMENTS

The $[arguments] placeholder.

public static final String
PLACEHOLDER_EXCEPTION

The $[exception] placeholder.

public static final String
PLACEHOLDER_INVOCATION_TIME

The $[invocationTime] placeholder.

public static final String
PLACEHOLDER_METHOD_NAME

The $[methodName] placeholder.

public static final String
PLACEHOLDER_RETURN_VALUE

The $[returnValue] placeholder.

public static final String
PLACEHOLDER_TARGET_CLASS_NAME

The $[targetClassName] placeholder.

public static final String
PLACEHOLDER_TARGET_CLASS_SHORT_NAME

The $[targetClassShortName] placeholder.

Inherited from org.springframework.aop.interceptor.AbstractTraceInterceptor:
defaultLogger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static void
appendArgumentTypes(MethodInvocation
the MethodInvocation being logged. Arguments will be retrieved from the corresponding Method.
methodInvocation
,
Matcher
the Matcher containing the state of the output
matcher
,
StringBuilder
the StringBuilder containing the output
output
)

Adds a comma-separated list of the short Class names of the method argument types to the output.

private static void
appendReturnValue(MethodInvocation
the MethodInvocation that returned the value
methodInvocation
,
Matcher
the Matcher containing the matched placeholder
matcher
,
StringBuilder
the StringBuilder to write output to
output
,
Object
the value returned by the method invocation.
returnValue
)

Adds the String representation of the method return value to the supplied StringBuilder.

private static void
checkForInvalidPlaceholders(String message)

Checks to see if the supplied String has any placeholders that are not specified as constants on this class and throws an IllegalArgumentException if so.

protected Object
invokeUnderTrace(MethodInvocation invocation, Log
the Log to write trace messages to
logger
)

Implements abstract org.springframework.aop.interceptor.AbstractTraceInterceptor.invokeUnderTrace.

Writes a log message before the invocation based on the value of enterMessage.

protected String

Returns:

the formatted output to write to the log
replacePlaceholders
(String
the message template containing the placeholders to be replaced
message
,
MethodInvocation
the MethodInvocation being logged. Used to derive values for all placeholders except $[exception] and $[returnValue].
methodInvocation
,
Object
any value returned by the invocation. Used to replace the $[returnValue] placeholder. May be null.
returnValue
,
Throwable
any Throwable raised during the invocation. The value of Throwable.toString() is replaced for the $[exception] placeholder. May be null.
throwable
,
long
the value to write in place of the $[invocationTime] placeholder
invocationTime
)

Replace the placeholders in the given message with the supplied values, or values derived from those supplied.

public void
setEnterMessage(String enterMessage)

Set the template used for method entry log messages.

public void
setExceptionMessage(String exceptionMessage)

Set the template used for method exception log messages.

public void
setExitMessage(String exitMessage)

Set the template used for method exit log messages.

Inherited from org.springframework.aop.interceptor.AbstractTraceInterceptor:
getClassForLogginggetLoggerForInvocationinvokeisInterceptorEnabledisLogEnabledsetHideProxyClassNamessetLogExceptionStackTracesetLoggerNamesetUseDynamicLoggerwriteToLogwriteToLog

Field Detail

ALLOWED_PLACEHOLDERSback to summary
pack-priv static final Set<String> ALLOWED_PLACEHOLDERS

The Set of allowed placeholders.

DEFAULT_ENTER_MESSAGEback to summary
private static final String DEFAULT_ENTER_MESSAGE

The default message used for writing method entry messages.

DEFAULT_EXCEPTION_MESSAGEback to summary
private static final String DEFAULT_EXCEPTION_MESSAGE

The default message used for writing exception messages.

DEFAULT_EXIT_MESSAGEback to summary
private static final String DEFAULT_EXIT_MESSAGE

The default message used for writing method exit messages.

enterMessageback to summary
private String enterMessage

The message for method entry.

exceptionMessageback to summary
private String exceptionMessage

The message for exceptions during method execution.

exitMessageback to summary
private String exitMessage

The message for method exit.

PATTERNback to summary
private static final Pattern PATTERN

The Pattern used to match placeholders.

PLACEHOLDER_ARGUMENT_TYPESback to summary
public static final String PLACEHOLDER_ARGUMENT_TYPES

The $[argumentTypes] placeholder. Replaced with a comma-separated list of the argument types for the method invocation. Argument types are written as short class names.

PLACEHOLDER_ARGUMENTSback to summary
public static final String PLACEHOLDER_ARGUMENTS

The $[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.

PLACEHOLDER_EXCEPTIONback to summary
public static final String PLACEHOLDER_EXCEPTION

The $[exception] placeholder. Replaced with the String representation of any Throwable raised during method invocation.

PLACEHOLDER_INVOCATION_TIMEback to summary
public static final String PLACEHOLDER_INVOCATION_TIME

The $[invocationTime] placeholder. Replaced with the time taken by the invocation (in milliseconds).

PLACEHOLDER_METHOD_NAMEback to summary
public static final String PLACEHOLDER_METHOD_NAME

The $[methodName] placeholder. Replaced with the name of the method being invoked.

PLACEHOLDER_RETURN_VALUEback to summary
public static final String PLACEHOLDER_RETURN_VALUE

The $[returnValue] placeholder. Replaced with the String representation of the value returned by the method invocation.

PLACEHOLDER_TARGET_CLASS_NAMEback to summary
public static final String PLACEHOLDER_TARGET_CLASS_NAME

The $[targetClassName] placeholder. Replaced with the fully-qualified name of the Class of the method invocation target.

PLACEHOLDER_TARGET_CLASS_SHORT_NAMEback to summary
public static final String PLACEHOLDER_TARGET_CLASS_SHORT_NAME

The $[targetClassShortName] placeholder. Replaced with the short name of the Class of the method invocation target.

Constructor Detail

CustomizableTraceInterceptorback to summary
public CustomizableTraceInterceptor()

Method Detail

appendArgumentTypesback to summary
private static void appendArgumentTypes(MethodInvocation methodInvocation, Matcher matcher, StringBuilder output)

Adds a comma-separated list of the short Class names of the method argument types to the output. For example, if a method has signature put(java.lang.String, java.lang.Object) then the value returned will be String, Object.

Parameters
methodInvocation:MethodInvocation

the MethodInvocation being logged. Arguments will be retrieved from the corresponding Method.

matcher:Matcher

the Matcher containing the state of the output

output:StringBuilder

the StringBuilder containing the output

appendReturnValueback to summary
private static void appendReturnValue(MethodInvocation methodInvocation, Matcher matcher, StringBuilder output, Object returnValue)

Adds the String representation of the method return value to the supplied StringBuilder. Correctly handles null and void results.

Parameters
methodInvocation:MethodInvocation

the MethodInvocation that returned the value

matcher:Matcher

the Matcher containing the matched placeholder

output:StringBuilder

the StringBuilder to write output to

returnValue:Object

the value returned by the method invocation.

checkForInvalidPlaceholdersback to summary
private static void checkForInvalidPlaceholders(String message) throws IllegalArgumentException

Checks to see if the supplied String has any placeholders that are not specified as constants on this class and throws an IllegalArgumentException if so.

invokeUnderTraceback to summary
protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable

Implements abstract org.springframework.aop.interceptor.AbstractTraceInterceptor.invokeUnderTrace.

Writes a log message before the invocation based on the value of 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.

Parameters
logger:Log

Doc from org.springframework.aop.interceptor.AbstractTraceInterceptor.invokeUnderTrace.

the Log to write trace messages to

Returns:Object

Doc from org.springframework.aop.interceptor.AbstractTraceInterceptor.invokeUnderTrace.

the result of the call to MethodInvocation.proceed()

Annotations
@Override
Exceptions
Throwable:

Doc from org.springframework.aop.interceptor.AbstractTraceInterceptor.invokeUnderTrace.

if the call to MethodInvocation.proceed() encountered any errors

See Also
setEnterMessage, setExitMessage, setExceptionMessage
replacePlaceholdersback to summary
protected String replacePlaceholders(String message, MethodInvocation methodInvocation, Object returnValue, Throwable throwable, long invocationTime)

Replace the placeholders in the given message with the supplied values, or values derived from those supplied.

Parameters
message:String

the message template containing the placeholders to be replaced

methodInvocation:MethodInvocation

the MethodInvocation being logged. Used to derive values for all placeholders except $[exception] and $[returnValue].

returnValue:Object

any value returned by the invocation. Used to replace the $[returnValue] placeholder. May be null.

throwable:Throwable

any Throwable raised during the invocation. The value of Throwable.toString() is replaced for the $[exception] placeholder. May be null.

invocationTime:long

the value to write in place of the $[invocationTime] placeholder

Returns:String

the formatted output to write to the log

setEnterMessageback to summary
public void setEnterMessage(String enterMessage) throws IllegalArgumentException

Set the template used for method entry log messages. This template can contain any of the following placeholders:

  • $[targetClassName]
  • $[targetClassShortName]
  • $[argumentTypes]
  • $[arguments]
setExceptionMessageback to summary
public void setExceptionMessage(String exceptionMessage)

Set the template used for method exception log messages. This template can contain any of the following placeholders:

  • $[targetClassName]
  • $[targetClassShortName]
  • $[argumentTypes]
  • $[arguments]
  • $[exception]
setExitMessageback to summary
public void setExitMessage(String exitMessage)

Set the template used for method exit log messages. This template can contain any of the following placeholders:

  • $[targetClassName]
  • $[targetClassShortName]
  • $[argumentTypes]
  • $[arguments]
  • $[returnValue]
  • $[invocationTime]