简体   繁体   English

如何正确调试 Spring 事件?

[英]How to properly debug Spring Events?

i'm starting to use Spring Events (using custom events and @EventListener annotation).我开始使用 Spring 事件(使用自定义事件和@EventListener注释)。 One of my co-worker asked me if it wouldn't be good to add a log line in the @EventListener method.我的一位同事问我在@EventListener方法中添加日志行是否不好。 So i started to look if there were any "good practices" to debugging Spring Event instead of adding log lines everywhere.因此,我开始查看是否有任何“良好做法”来调试 Spring 事件,而不是到处添加日志行。 Kind of a generic way to know which and when events were send and which listener treated them.一种通用的方式来了解哪些事件以及何时发送以及哪个侦听器处理了它们。

Put in simple terms: How to properly trace and debug Spring Event usages?简而言之:如何正确跟踪和调试 Spring 事件用法?

Could not find any blog, article or StackOverflow discussion on the subject.找不到关于该主题的任何博客、文章或 StackOverflow 讨论。 Could someone point me in the right direction?有人能指出我正确的方向吗?

Thank you !谢谢 !

  • You can create another logging event listener that listens for the events you specify and logs.您可以创建另一个日志事件侦听器来侦听您指定并记录的事件。
  @Component
  public class LoggingEventListener {
  
    @EventListener(classes=...)
    public void logEvent(ApplicationEvent event) {
        ...
    }
  }
  • Or you can write log aspect based on the @EventListener annotation and it logs it.或者您可以基于@EventListener注释编写日志aspect并将其记录下来。

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

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