Debug your event listeners and subscribers easily in Symfony

Ever found yourself stuck in a stack of subscribers and listeners, unable to determine why and which class has modified your object after an event has been dispatched?

The old solution

Previously, you could search in your code for the name of the event. But even if you have a constant for the event name, sometimes listeners are registered directly in the Dependency Injection Container, or worse, dynamically.

Furthermore, how to detect the execution order of the listeners? Does a higher priority imply an earlier execution? And what if two events have the same priority? You would have to know exactly how the framework works internally, and sometimes you just want a quick explanation of what is happening.

debug:event-dispatcher at the rescue

To face this, I proposed a new command in Symfony, available since version 2.6:

$ app/console debug:event-dispatcher

The default behavior is to list all the events in your application which have at least one registered listener/subscriber, and give you all theses listeners/subscribers ordered by their descending priority (i.e. their execution order).

[event_dispatcher] Registered listeners by event

[Event] console.command
+-------+-------------------------------------------------------------------------------+
| Order | Callable                                                                      |
+-------+-------------------------------------------------------------------------------+
| #1    | Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure() |
| #2    | Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand()                    |
| #3    | Symfony\Bridge\Monolog\Handler\ConsoleHandler::onCommand()                    |
+-------+-------------------------------------------------------------------------------+

[Event] console.terminate
+-------+-----------------------------------------------------------------------------------+
| Order | Callable                                                                          |
+-------+-----------------------------------------------------------------------------------+
| #1    | Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener::onTerminate() |
.
.
.

The command fetches any listeners/subscribers, even if they are registered dynamically and not declared as services.

Debug a specific event

If you want to debug a specific event, just give the event name to the command:

app/console debug:event-dispatcher kernel.request

[event_dispatcher] Registered listeners for event kernel.request

+-------+----------------------------------------------------------------------------------+
| Order | Callable                                                                         |
+-------+----------------------------------------------------------------------------------+
| #1    | Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure()    |
| #2    | Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest()   |
| #3    | Symfony\Component\HttpKernel\EventListener\DumpListener::configure()             |
| #4    | Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest()  |
| #5    | Symfony\Component\HttpKernel\EventListener\FragmentListener::onKernelRequest()   |
| #6    | Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest()     |
| #7    | Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest()     |
| #8    | Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest() |
| #9    | Symfony\Component\Security\Http\Firewall::onKernelRequest()                      |
| #10   | Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest()    |
+-------+----------------------------------------------------------------------------------+

Feedback and contributions welcome

Do you find this command useful? Do you have suggestions to improve the readability? Feel free to comment here, open a issue or propose a Pull Request on Github.


You liked this article? You'd probably be a good match for our ever-growing tech team at Theodo.

Join Us