类 CustomizableTraceInterceptor
java.lang.Object
infra.aop.interceptor.AbstractTraceInterceptor
infra.aop.interceptor.CustomizableTraceInterceptor
- 所有已实现的接口:
Serializable,Advice,Interceptor,MethodInterceptor
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 theStringrepresentation of the method arguments$[exception]- replaced with theStringrepresentation of anyThrowableraised during the invocation$[invocationTime]- replaced with the time, in milliseconds, taken by the method invocation
There are restrictions on which placeholders can be used in which messages: see the individual message properties for details on the valid placeholders.
- 从以下版本开始:
- 3.0
- 作者:
- TODAY, Rob Harrop, Juergen Hoeller
- 另请参阅:
-
字段概要
字段修饰符和类型字段说明TheSetof allowed placeholders.private static final StringThe default message used for writing method entry messages.private static final StringThe default message used for writing exception messages.private static final StringThe default message used for writing method exit messages.private StringThe message for method entry.private StringThe message for exceptions during method execution.private StringThe message for method exit.private static final PatternThePatternused to match placeholders.static final StringThe$[argumentTypes]placeholder.static final StringThe$[arguments]placeholder.static final StringThe$[exception]placeholder.static final StringThe$[invocationTime]placeholder.static final StringThe$[methodName]placeholder.static final StringThe$[returnValue]placeholder.static final StringThe$[targetClassName]placeholder.static final StringThe$[targetClassShortName]placeholder.从类继承的字段 infra.aop.interceptor.AbstractTraceInterceptor
defaultLogger -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明private static voidappendArgumentTypes(MethodInvocation methodInvocation, Matcher matcher, StringBuilder output) Adds a comma-separated list of the shortClassnames of the method argument types to the output.private static voidappendReturnValue(MethodInvocation methodInvocation, Matcher matcher, StringBuilder output, Object returnValue) Adds theStringrepresentation of the method return value to the suppliedStringBuilder.private static voidcheckForInvalidPlaceholders(String message) Checks to see if the suppliedStringhas any placeholders that are not specified as constants on this class and throws anIllegalArgumentExceptionif so.protected ObjectinvokeUnderTrace(MethodInvocation invocation, infra.logging.Logger logger) Writes a log message before the invocation based on the value ofenterMessage.protected StringreplacePlaceholders(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.voidsetEnterMessage(String enterMessage) Set the template used for method entry log messages.voidsetExceptionMessage(String exceptionMessage) Set the template used for method exception log messages.voidsetExitMessage(String exitMessage) Set the template used for method exit log messages.从类继承的方法 infra.aop.interceptor.AbstractTraceInterceptor
getClassForLogging, getLoggerForInvocation, invoke, isInterceptorEnabled, isLogEnabled, setHideProxyClassNames, setLogExceptionStackTrace, setLoggerName, setUseDynamicLogger, writeToLog, writeToLog
-
字段详细资料
-
PLACEHOLDER_METHOD_NAME
The$[methodName]placeholder. Replaced with the name of the method being invoked.- 另请参阅:
-
PLACEHOLDER_TARGET_CLASS_NAME
The$[targetClassName]placeholder. Replaced with the fully-qualified name of theClassof the method invocation target.- 另请参阅:
-
PLACEHOLDER_TARGET_CLASS_SHORT_NAME
The$[targetClassShortName]placeholder. Replaced with the short name of theClassof the method invocation target.- 另请参阅:
-
PLACEHOLDER_RETURN_VALUE
The$[returnValue]placeholder. Replaced with theStringrepresentation of the value returned by the method invocation.- 另请参阅:
-
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_ARGUMENTS
The$[arguments]placeholder. Replaced with a comma separated list of the argument values for the method invocation. Relies on thetoString()method of each argument type.- 另请参阅:
-
PLACEHOLDER_EXCEPTION
The$[exception]placeholder. Replaced with theStringrepresentation of anyThrowableraised during method invocation.- 另请参阅:
-
PLACEHOLDER_INVOCATION_TIME
The$[invocationTime]placeholder. Replaced with the time taken by the invocation (in milliseconds).- 另请参阅:
-
DEFAULT_ENTER_MESSAGE
The default message used for writing method entry messages.- 另请参阅:
-
DEFAULT_EXIT_MESSAGE
The default message used for writing method exit messages.- 另请参阅:
-
DEFAULT_EXCEPTION_MESSAGE
The default message used for writing exception messages.- 另请参阅:
-
PATTERN
ThePatternused to match placeholders. -
ALLOWED_PLACEHOLDERS
TheSetof allowed placeholders. -
enterMessage
The message for method entry. -
exitMessage
The message for method exit. -
exceptionMessage
The message for exceptions during method execution.
-
-
构造器详细资料
-
CustomizableTraceInterceptor
public CustomizableTraceInterceptor()
-
-
方法详细资料
-
setEnterMessage
Set the template used for method entry log messages. This template can contain any of the following placeholders:$[targetClassName]$[targetClassShortName]$[argumentTypes]$[arguments]
-
setExitMessage
Set the template used for method exit log messages. This template can contain any of the following placeholders:$[targetClassName]$[targetClassShortName]$[argumentTypes]$[arguments]$[returnValue]$[invocationTime]
-
setExceptionMessage
Set the template used for method exception log messages. This template can contain any of the following placeholders:$[targetClassName]$[targetClassShortName]$[argumentTypes]$[arguments]$[exception]
-
invokeUnderTrace
protected Object invokeUnderTrace(MethodInvocation invocation, infra.logging.Logger logger) throws Throwable Writes a log message before the invocation based on the value ofenterMessage. If the invocation succeeds, then a log message is written on exit based on the valueexitMessage. If an exception occurs during invocation, then a message is written based on the value ofexceptionMessage.- 指定者:
invokeUnderTrace在类中AbstractTraceInterceptorlogger- theLoggerto write trace messages to- 返回:
- the result of the call to
MethodInvocation.proceed() - 抛出:
Throwable- if the call toMethodInvocation.proceed()encountered any errors- 另请参阅:
-
replacePlaceholders
protected String replacePlaceholders(String message, MethodInvocation methodInvocation, @Nullable Object returnValue, @Nullable Throwable throwable, long invocationTime) Replace the placeholders in the given message with the supplied values, or values derived from those supplied.- 参数:
message- the message template containing the placeholders to be replacedmethodInvocation- theMethodInvocationbeing 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 benull.throwable- anyThrowableraised during the invocation. The value ofThrowable.toString()is replaced for the$[exception]placeholder. May benull.invocationTime- the value to write in place of the$[invocationTime]placeholder- 返回:
- the formatted output to write to the log
-
appendReturnValue
private static void appendReturnValue(MethodInvocation methodInvocation, Matcher matcher, StringBuilder output, @Nullable Object returnValue) Adds theStringrepresentation of the method return value to the suppliedStringBuilder. Correctly handlesnullandvoidresults.- 参数:
methodInvocation- theMethodInvocationthat returned the valuematcher- theMatchercontaining the matched placeholderoutput- theStringBuilderto write output toreturnValue- the value returned by the method invocation.
-
appendArgumentTypes
private static void appendArgumentTypes(MethodInvocation methodInvocation, Matcher matcher, StringBuilder output) Adds a comma-separated list of the shortClassnames of the method argument types to the output. For example, if a method has signatureput(java.lang.String, java.lang.Object)then the value returned will beString, Object.- 参数:
methodInvocation- theMethodInvocationbeing logged. Arguments will be retrieved from the correspondingMethod.matcher- theMatchercontaining the state of the outputoutput- theStringBuildercontaining the output
-
checkForInvalidPlaceholders
Checks to see if the suppliedStringhas any placeholders that are not specified as constants on this class and throws anIllegalArgumentExceptionif so.
-