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

我已经find了很多关于如何使用这个或者这个 Spring框架为日志logging创build一个自定义方面的例子,但是没有为这种情况和问题find标准/常见的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> 

查看CustomizableTraceInterceptor API,您可以使用多个占位符定义单独的input/退出/exception消息:

  • $[methodName] – 用正在调用的方法的名称replace
  • $[targetClassName] – 用作为调用目标的类的名称replace
  • $[targetClassShortName] – 用作为调用目标的类的短名称replace
  • $[returnValue] – 用调用返回的值replace
  • $[argumentTypes] – 用逗号分隔的方法参数的短类名称replace
  • $[arguments] – 用方法参数的string表示forms的逗号分隔列表replace
  • $[exception] – 用调用期间引发的任何Throwable的string表示replace
  • $[invocationTime] – 用方法调用所用的时间(以毫秒为单位)replace

以下是通过AOP进行日志logging的框架列表:

http://aspect4log.sf.net – 通过slf4j和@Log注释非常好看。 可以通过SpringAOP和AspectJ工作。 使用AspectJ,它甚至可以用于私有方法和构造函数,并且不需要类就可以成为Spring Bean。 使用起来非常简单,我可以在5分钟内完成项目的运行。

http://loggifier.unkrig.de – 通过java.util.logging进行日志logging,有点太复杂,不是那么好的文档,但声称它可以testing已经编译过的jar / war / ear文件!

AbstractTraceInterceptor(来自SpringAOP)及其子类SimpleTraceInterceptor和CustomizableTraceInterceptor。 日志loggingconfiguration与类别分开进行。 通过公共日志logging日志。 由于它是为SpringAOPdevise的,所以您必须使用Spring Beans(并且只能使用Spring Beans公共方法)。