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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7302090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:28:20  来源:igfitidea点击:

Spring: Standard Logging aspect (interceptor)

javaspringloggingaspectj

提问by Sergii Zagriichuk

I've found a lot of examples on how to create a custom aspect for logging using the Spring framework like thisor thisbut did not find standard/common Spring implementation for this situation and question. Are there any standard implementations of logging aspect from Spring or not?

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

回答by Tomasz Nurkiewicz

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 CustomizableTraceInterceptorAPI, you can define separate enter/exit/exception messages with several placeholders:

查看CustomizableTraceInterceptorAPI,您可以使用多个占位符定义单独的进入/退出/异常消息:

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

回答by Mark D

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. Can work via SpringAOP, and AspectJ. With AspectJ it works even for private methods and constructors and does not require a class to be a Spring Bean. Very easy to use, i was able to make it running with my project within 5 min.

http://aspect4log.sf.net- 通过 slf4j 和 @Log 注释做非常漂亮的日志记录。可以通过 SpringAOP 和 AspectJ 工作。使用 AspectJ,它甚至适用于私有方法和构造函数,并且不需要类是 Spring Bean。非常易于使用,我能够在 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. 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).

AbstractTraceInterceptor(来自 SpringAOP)及其子类 SimpleTraceInterceptor 和 CustomizableTraceInterceptor。日志配置与类分开完成。通过 commons-logging 记录日志。由于它是为 SpringAOP 设计的,因此您必须使用 Spring Beans(并且只能使用 Spring Beans 公共方法)。