我发现了很多关于如何使用 Spring 框架创建自定义方面进行日志记录的示例,例如this or this但没有找到这种情况和问题的标准/通用 Spring 实现。 Spring 是否有日志记录方面的标准实现?
是的,有!
<bean id="customizableTraceInterceptor" class="org.springframework.aop.interceptor.CustomizableTraceInterceptor">
<property name="enterMessage" value="Entering $[methodName]($[arguments])"/>
<property name="exitMessage" value="Leaving $[methodName](): $[returnValue]"/>
</bean>
<aop:config>
<aop:advisor advice-ref="customizableTraceInterceptor" pointcut="execution(public * BankAccountServlet.*(..))"/>
</aop:config>
查看可定制的Trace拦截器API,您可以使用多个占位符定义单独的进入/退出/异常消息:
-
$[methodName]
- 替换为正在调用的方法的名称
-
$[targetClassName]
- 替换为调用目标的类的名称
-
$[targetClassShortName]
- 替换为调用目标类的短名称
-
$[returnValue]
- 替换为调用返回的值
-
$[argumentTypes]
- 替换为方法参数的短类名的逗号分隔列表
-
$[arguments]
- 替换为方法参数的字符串表示形式的逗号分隔列表
-
$[exception]
- 替换为调用期间引发的任何 Throwable 的字符串表示形式
-
$[invocationTime]
- 替换为方法调用所用的时间(以毫秒为单位)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)