spring-framework / org.springframework.aop.interceptor / CustomizableTraceInterceptor

CustomizableTraceInterceptor

open class CustomizableTraceInterceptor : 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:

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

Author
Rob Harrop

Author
Juergen Hoeller

Since
1.2

See Also
#setEnterMessage#setExitMessage#setExceptionMessageSimpleTraceInterceptor

Constructors

<init>

CustomizableTraceInterceptor()

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 invocation

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

Properties

PLACEHOLDER_ARGUMENTS

static val PLACEHOLDER_ARGUMENTS: String

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_ARGUMENT_TYPES

static val PLACEHOLDER_ARGUMENT_TYPES: String

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_EXCEPTION

static val PLACEHOLDER_EXCEPTION: String

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

PLACEHOLDER_INVOCATION_TIME

static val PLACEHOLDER_INVOCATION_TIME: String

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

PLACEHOLDER_METHOD_NAME

static val PLACEHOLDER_METHOD_NAME: String

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

PLACEHOLDER_RETURN_VALUE

static val PLACEHOLDER_RETURN_VALUE: String

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

PLACEHOLDER_TARGET_CLASS_NAME

static val PLACEHOLDER_TARGET_CLASS_NAME: String

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

PLACEHOLDER_TARGET_CLASS_SHORT_NAME

static val PLACEHOLDER_TARGET_CLASS_SHORT_NAME: String

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

Functions

setEnterMessage

open fun setEnterMessage(enterMessage: String): Unit

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

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

setExceptionMessage

open fun setExceptionMessage(exceptionMessage: String): Unit

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

  • $[targetClassName]
  • $[targetClassShortName]
  • $[argumentTypes]
  • $[arguments]
  • $[exception]

setExitMessage

open fun setExitMessage(exitMessage: String): Unit

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

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

Inherited Functions

invoke

open fun invoke(invocation: MethodInvocation): Any

Determines whether or not 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.

setHideProxyClassNames

open fun setHideProxyClassNames(hideProxyClassNames: Boolean): Unit

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

setLogExceptionStackTrace

open fun setLogExceptionStackTrace(logExceptionStackTrace: Boolean): Unit

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

setLoggerName

open fun setLoggerName(loggerName: String): Unit

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.

setUseDynamicLogger

open fun setUseDynamicLogger(useDynamicLogger: Boolean): Unit

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.