15

In Magento 1 I can get list of events/observers by debugging dispatchEvent() method from Mage.php like below.

/**
     * Dispatch event
     *
     * Calls all observer callbacks registered for this event
     * and multiple observers matching event name pattern
     *
     * @param string $name
     * @param array $data
     * @return Mage_Core_Model_App
     */
    public static function dispatchEvent($name, array $data = array())
    {
        Mage::log($name,null,'Events');
        Varien_Profiler::start('DISPATCH EVENT:'.$name);
        $result = self::app()->dispatchEvent($name, $data);
        Varien_Profiler::stop('DISPATCH EVENT:'.$name);
        return $result;
    }

In magento 2 where I can get list of events/observers?

14

You can do the same thing you did in Magento 1.x in the method \Magento\Framework\Event\Manager::dispatch().

but it's a difference. You don't have access to the logger.
You will have to inject an instance of the logger in the constructor.

protected $logger;

public function __construct(
    InvokerInterface $invoker, 
    ConfigInterface $eventConfig,
    \Psr\Log\LoggerInterface $logger
)
{
    $this->_invoker = $invoker;
    $this->_eventConfig = $eventConfig;
    $this->logger = $logger;
}

Then you can call in the dispatch method this:

$this->logger->info($message);

Instead of info you can use all the methods from \Psr\Log\LoggerInterface

  • You are rocking........ – Bojjaiah Oct 15 '15 at 5:56
  • @Marius just a typo with keyword $protected instead of protected $logger. – Haijerome Apr 13 '16 at 6:33
3

In my case I can get list of all event by doing below changes which is very short cut like we do in mage.php file of magento1:

Note: I have only tested on magento2.1.1 version so I am not sure for any other version

\vendor\magento\framework\Event\Manager.php

public function dispatch

write below code to get all event in debug.log file after

$eventName = mb_strtolower($eventName); 

near to line 56

\Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->debug($eventName);
3

Since this is for "quick debugging", you could avoid multiple edits by doing.

public function dispatch($eventName, array $data = [])
{
    $logger = \Magento\Framework\App\ObjectManager::getInstance()->get(\Psr\Log\LoggerInterface::class);
    $logger->info($eventName);
    ...

Location

/lib/internal/Magento/Framework/Event/Manager.php

@Marius answer is the correct solution.

  • Use \Psr\Log\LoggerInterface::class please. Always. – nevvermind Nov 19 '15 at 17:17
  • @nevvermind .. I tried that before ... Fatal error: Non-static method Psr\Log\LoggerInterface::info() cannot be called statically. Please let me know if you figure out an easier way. – Renon Stewart Nov 19 '15 at 17:26
  • I'm talking about the ::class keyword instead of the literal string FQCN. – nevvermind Nov 19 '15 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.