How to determine where an event was sent to Magento?

I have an extension using the catalog_product_after_save event to make a simple index of products and attributes for custom export.

It seems that some cases where this event is dispatched without all the product data, so when the index is updated, it skips the data. I put a check that will look for some required information that is missing, so I believe that this will not be a problem, but I would like to register when this will happen and where it came from.

So, is there a way to determine where the event was sent from, from which I can look in the observer?

+4
source share
3 answers

Get to debug_backtrace PHP backtrace functions ( debug_backtrace and debug_print_backtrace ) or, even better, a secure version of the Magento / large object, mageDebugBacktrace .

For example, I have an observer setting for the controller_action_predispatch event. If I put the following in my observer (since the observer can be called twice, you may not want to exit for your specific case)

 class Pulsestorm_Requestset_Model_Observer { public function myMethod($observer) { mageDebugBacktrace(); exit; } } 

and then load the page, I will get the following result when I try to load any page on the system (since this event fires on almost every page)

 [1] /magento/app/code/core/Mage/Core/Model/App.php:1343 [2] /magento/app/code/core/Mage/Core/Model/App.php:1322 [3] /magento/app/Mage.php:455 [4] /magento/app/code/core/Mage/Core/Controller/Varien/Action.php:530 [5] /magento/app/code/core/Mage/Core/Controller/Front/Action.php:64 [6] /magento/app/code/core/Mage/Core/Controller/Varien/Action.php:408 [7] /magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php:251 [8] /magento/app/code/core/Mage/Core/Controller/Varien/Front.php:176 [9] /magento/app/code/core/Mage/Core/Model/App.php:352 [10] /magento/app/Mage.php:691 [11] /magento/index.php:87 

This gives me a simplified column to the point I called mageDebugBacktrace . [#] is just the ordering of the list, followed by the line : delimited. The left side of the line ( /magento/app/code/core/Mage/Core/Model/App.php ) is a PHP file, the right side of the line ( 1343 ) is the number of the line where the method was called .

First three calls

 [1] /magento/app/code/core/Mage/Core/Model/App.php:1343 [2] /magento/app/code/core/Mage/Core/Model/App.php:1322 [3] /magento/app/Mage.php:455 

is the PHP code that sent this event. For example, line 455 Mage.php on my system

 $result = self::app()->dispatchEvent($name, $data); 

This (usually depending on the version of Magento / state of your kernel) is the fourth call on the stack that will indicate where the event was sent.

 [4] /magento/app/code/core/Mage/Core/Controller/Varien/Action.php:530 

line 530 of Action.php on my system

 Mage::dispatchEvent('controller_action_predispatch', array('controller_action' => $this)); 

Bingo! Mage::dispatchEvent is the code that dispatches the event, so we found our dispatch point.

As already mentioned, your observer can be called several times - therefore, data logging with output buffering may be better than the output / output used above

  ob_start(); mageDebugBacktrace(); $contents = ob_get_clean(); Mage::Log($contents); #If `Mage::log` is failing early in the bootstrap process #file_put_contents('/tmp/trace.log', $contents . "\n",FILE_APPEND); 
+10
source

Here is the definition of the mageDebugBacktrace function:

function mageDebugBacktrace ($ return = false, $ html = true, $ showFirst = false)

So, considering this, Alan’s example could be simplified like this:

 $trace = mageDebugBacktrace(true, false); Mage::log($trace, null, 'log-name', true); 
+3
source

The easiest method I know is to make a log. In app / Mage.php find this function

 public static function dispatchEvent($name, array $data = array()) { 

And after that add this line

 Mage::log($name, null, 'event_check.log', true); 

what the function will look like.

 public static function dispatchEvent($name, array $data = array()) { Mage::log($name, null, 'event_check.log', true); Varien_Profiler::start('DISPATCH EVENT:'.$name); $result = self::app()->dispatchEvent($name, $data); #$result = self::registry('events')->dispatch($name, $data); Varien_Profiler::stop('DISPATCH EVENT:'.$name); return $result; } 

Thus, you save a track for each event sent.

+1
source

All Articles