Description
Claus Ibsen opened SPR-601 and commented
I forked the org.springframework.aop.interceptors.TraceInterceptor as I needed several added functionality. I ended up adding a few more features and made it customizable so I could contribute it back to the Spring team.
I have attached a modified version of the TraceInterceptor.java with these enhancements:
- possible to customize output formating of ENTER and EXIT messages
- possible to output elapsed time
- possible to output method arguments
- possible to use dynamic logger name (using the classname of the method invocation = just as if you had written to log code in the class itself. See example)
Using the dynamic logname makes it possible to output logs as:
http8080-Processor4 07 jan 2005 11:22:58 DEBUG dk.netfragt.pbs.NetfragtPBSWebService - ENTER - accepterFaktura
dk.netfragt.pbs.NetfragtPBSWebService - EXIT - accepterFaktura. Time = 1656 msec.
Instead of the default:
http8080-Processor4 07 jan 2005 11:19:43 DEBUG org.springframework.aop.interceptor.TraceInterceptor - Entering method accepterFaktura in class [dk.netfragt.pbs.NetfragtPBSWebService]
And with the method arguments you can see what has been passed in as arguments (in this example 77777):
http8080-Processor4 07 jan 2005 11:19:43 DEBUG org.springframework.aop.interceptor.TraceInterceptor - Entering method accepterFaktura(77777) in class [dk.netfragt.pbs.NetfragtPBSWebService]
And you can change the formatting of the message using java.text.Format with {0} placeholders. The defaults are:
private String enterMessage = "Entering method {1}{2} in class [{0}]";
private String exitMessage = "Exiting method {1}{2} in class [{0}]";
You can customize this in the spring .xml configuration:
<bean id="traceInterceptor" class="org.springframework.aop.interceptor.TraceInterceptor">
<property name="elapsedTime"><value>true</value></property>
<property name="arguments"><value>true</value></property>
<property name="dynamicLogName"><value>true</value></property>
<property name="enterMessage"><value>ENTER - {1}{2}</value></property>
<property name="exitMessage"><value>EXIT - {1}{2}</value></property>
</bean>
Using the above will output:
http8080-Processor4 07 jan 2005 11:22:58 DEBUG dk.netfragt.pbs.NetfragtPBSWebService - ENTER - accepterFaktura(676767)
http8080-Processor4 07 jan 2005 11:23:00 DEBUG dk.netfragt.pbs.NetfragtPBSWebService - EXIT - accepterFaktura(676767). Time = 1656 msec.
As the example above illustrates that using dynamicLogName and customizing the log message it is possible to change it radically.
I hope the Spring team could accept these changes as the default version is the same as the existing one.
Affects: 1.1.3
Attachments:
- TraceInterceptor.java (7.50 kB)
- TraceInterceptor.java (5.55 kB)
Issue Links:
- [PATCH] Enhancements to TraceInterceptor [SPR-860] #5586 [PATCH] Enhancements to TraceInterceptor ("is duplicated by")