Spring:标准日志方面(拦截器)

时间:2011-09-04 20:57:55

标签: java spring logging aspectj

我已经找到很多关于如何使用Spring框架(如thisthis创建自定义方面的示例,但是没有找到针对这种情况和问题的标准/通用Spring实现。 Spring中是否有任何标准的日志记录方面实现?

2 个答案:

答案 0 :(得分:24)

是的!

<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] - 替换为方法调用
  • 占用的时间(以毫秒为单位)   

答案 1 :(得分:0)

以下是通过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公共方法)。

相关问题