How to properly debug Spring Events?

Viewed 671

i'm starting to use Spring Events (using custom events and @EventListener annotation). One of my co-worker asked me if it wouldn't be good to add a log line in the @EventListener method. So i started to look if there were any "good practices" to debugging Spring Event instead of adding log lines everywhere. 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 ?

Could not find any blog, article or StackOverflow discussion on the subject. Could someone point me in the right direction ?

Thank you !

1 Answers
  • 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.
Related