merged branch aboks/event_trace (PR #1786)

Commits
-------

9d8e6f6 [FrameworkBundle] Changed TraceableEventDispatcher to log calls to event listeners _before_ actually calling them

Discussion
----------

[FrameworkBundle] Log calls to event listereners _before_ calling them

The current implementation of `TraceableEventDispatcher` logs calls to event listeners _after_ actually calling them. This leads to strange logs when an event listener triggers another event.

For example, if I attach some `LoginListener` to the `security.interactive_login`-event, the log will look something like this:
<pre>
...
User "myusername" has been authenticated successfully
Notified event "security.interactive_login" to listener "MyVendor\MyBundle\EventListener\LoginListener::onSecurityInteractiveLogin".
Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
...
</pre>
From the logs it looks like the `kernel.request` event was fired after the user was authenticated, whereas it was actually the listener to `kernel.request` that caused the user to be authenticated.

By logging the call to the event listener _before_ calling it, the logs will look like this:
<pre>
...
Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
Notified event "security.interactive_login" to listener "MyVendor\MyBundle\EventListener\LoginListener::onSecurityInteractiveLogin".
User "myusername" has been authenticated successfully
...
</pre>
In my opinion this makes the causal relationship between the events clearer.

---------------------------------------------------------------------------

by stof at 2011/07/24 11:18:48 -0700

👍 for this.
This commit is contained in:
Fabien Potencier 2011-07-26 09:24:48 +02:00
commit 5c13835d21

View File

@ -71,8 +71,6 @@ class TraceableEventDispatcher extends ContainerAwareEventDispatcher implements
protected function doDispatch($listeners, $eventName, Event $event)
{
foreach ($listeners as $listener) {
call_user_func($listener, $event);
$info = $this->getListenerInfo($listener, $eventName);
if (null !== $this->logger) {
@ -81,6 +79,8 @@ class TraceableEventDispatcher extends ContainerAwareEventDispatcher implements
$this->called[$eventName.'.'.$info['pretty']] = $info;
call_user_func($listener, $event);
if ($event->isPropagationStopped()) {
if (null !== $this->logger) {
$this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', $info['pretty'], $eventName));