【发布时间】:2011-11-10 06:04:27
【问题描述】:
我找到了很多关于如何使用 this 或 this 等 Spring 框架创建自定义日志方面的示例,但没有找到针对这种情况和问题的标准/通用 Spring 实现。是否有来自 Spring 的日志记录方面的任何标准实现?
【问题讨论】:
标签: java spring logging aspectj
我找到了很多关于如何使用 this 或 this 等 Spring 框架创建自定义日志方面的示例,但没有找到针对这种情况和问题的标准/通用 Spring 实现。是否有来自 Spring 的日志记录方面的任何标准实现?
【问题讨论】:
标签: java spring logging aspectj
有!
<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>
查看CustomizableTraceInterceptor API,您可以使用多个占位符定义单独的进入/退出/异常消息:
$[methodName]- 替换为被调用方法的名称$[targetClassName]- 替换为作为调用目标的类的名称$[targetClassShortName]- 替换为作为调用目标的类的短名称$[returnValue]- 替换为调用返回的值$[argumentTypes]- 替换为以逗号分隔的方法参数短类名列表$[arguments]- 替换为方法参数的字符串表示形式的逗号分隔列表$[exception]- 替换为调用期间引发的任何 Throwable 的 String 表示$[invocationTime]- 替换为方法调用所花费的时间,以毫秒为单位
【讨论】:
以下是通过 AOP 进行日志记录的框架列表:
http://aspect4log.sf.net - 通过 slf4j 和 @Log 注释进行非常漂亮的日志记录。 可以通过 SpringAOP 和 AspectJ 工作。使用 AspectJ,它甚至适用于私有方法和构造函数,并且不需要类是 Spring Bean。 非常易于使用,我能够在 5 分钟内使其与我的项目一起运行。
http://loggifier.unkrig.de - 通过 java.util.logging 进行日志记录,有点过于复杂,文档也不是很好,但声称它可以检测已编译的 jar/war/ear 文件!
AbstractTraceInterceptor(来自 SpringAOP)及其子类 SimpleTraceInterceptor 和 CustomizableTraceInterceptor。日志配置与类分开完成。通过公共日志记录日志。由于它是为 SpringAOP 设计的,因此您必须使用 Spring Beans(并且只能使用 Spring Beans 公共方法)。
【讨论】: