简体   繁体   English

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

[英]Spring: Standard Logging aspect (interceptor)

I've found a lot of examples on how to create a custom aspect for logging using the Spring framework like this or this but did not find standard/common Spring implementation for this situation and question. 我已经找到了很多关于如何使用像这样或者这样的Spring框架创建自定义方面的示例,但没有找到针对这种情况和问题的标准/常见Spring实现。 Are there any standard implementations of logging aspect from Spring or not? Spring是否有任何标准的日志记录方面实现?

Yes there are! 是的有!

<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>

Check out the CustomizableTraceInterceptor API, you can define separate enter/exit/exception messages with several placeholders: 查看CustomizableTraceInterceptor API,您可以使用多个占位符定义单独的输入/退出/异常消息:

  • $[methodName] - replaced with the name of the method being invoked $[methodName] - 替换为被调用方法的名称
  • $[targetClassName] - replaced with the name of the class that is the target of the invocation $[targetClassName] - 替换为作为调用目标的类的名称
  • $[targetClassShortName] - replaced with the short name of the class that is the target of the invocation $[targetClassShortName] - 替换为作为调用目标的类的短名称
  • $[returnValue] - replaced with the value returned by the invocation $[returnValue] - 替换为调用返回的值
  • $[argumentTypes] - replaced with a comma-separated list of the short class names of the method arguments $[argumentTypes] - 替换为逗号分隔的方法参数的短类名列表
  • $[arguments] - replaced with a comma-separated list of the String representation of the method arguments $[arguments] - 替换为方法参数的String表示的逗号分隔列表
  • $[exception] - replaced with the String representation of any Throwable raised during the invocation $[exception] - 替换为在调用期间引发的任何Throwable的String表示
  • $[invocationTime] - replaced with the time, in milliseconds, taken by the method invocation $[invocationTime] - 替换为方法调用所用的时间(以毫秒为单位)

Here are the list of frameworks that do logging via AOP: 以下是通过AOP进行日志记录的框架列表:

http://aspect4log.sf.net - does very nice looking logging via slf4j and @Log annotation. http://aspect4log.sf.net - 通过slf4j和@Log注释进行非常好看的日志记录。 Can work via SpringAOP, and AspectJ. 可以通过SpringAOP和AspectJ工作。 With AspectJ it works even for private methods and constructors and does not require a class to be a Spring Bean. 使用AspectJ,它甚至可以用于私有方法和构造函数,并且不需要类成为Spring Bean。 Very easy to use, i was able to make it running with my project within 5 min. 非常容易使用,我能够在5分钟内让它与我的项目一起运行。

http://loggifier.unkrig.de - does logging via java.util.logging, a bit too complex and not that well document but claims that it can instrument already compiled jar/war/ear files! http://loggifier.unkrig.de - 通过java.util.logging进行日志记录,有点过于复杂而且不是那么好的文档,但声称它可以记录已编译的jar / war / ear文件!

AbstractTraceInterceptor (from SpringAOP) and it's subclasses SimpleTraceInterceptor and CustomizableTraceInterceptor. AbstractTraceInterceptor(来自SpringAOP)和它的子类SimpleTraceInterceptor和CustomizableTraceInterceptor。 Logging configuration is done separately from classes. 记录配置与类分开完成。 Logs via commons-logging. 通过公共日志记录日志。 Since it is designed for SpringAOP you have to work with Spring Beans (and only with Spring Beans public methods). 由于它是为SpringAOP设计的,因此您必须使用Spring Beans(并且只能使用Spring Beans公共方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM