Skip to content

Instantly share code, notes, and snippets.

@molotovbliss
Created August 1, 2018 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save molotovbliss/2792c6c59410d75e5baf249b76413247 to your computer and use it in GitHub Desktop.
Save molotovbliss/2792c6c59410d75e5baf249b76413247 to your computer and use it in GitHub Desktop.
Debugging tips & tricks with Magento 1.x Commerce

Archive of Debugging tips & tricks with Magento Commerce

Originally posted from https://web.archive.org/web/20171207214530/http://www.molotovbliss.com/debugging-tips-and-tricks-with-magento-commerce

Zend_Debug::Dump

Zend_Debug::dump

Use Zend_Debug::dump($foo); instead of using var_dump($foo); or print_r($foo); it is essentially the same, just a wrapper with pre HTML tag for formatting and escaping of special characters.

However there will be times that simply dumping objects to the screen can be too much and cause browser hangups or even crashes. The best practice I’ve learned is to always use the getData() method that Magento has built-in to the Varien Object, this way your not getting redundant amounts of data dumped to the screen but only the bits you really care about. Varien Object getData, debug

Magento also has a built-in debug() method in the Varien Object as well that you can use to display data in a string representation of itself.

Keep in mind debug does NOT always get attached to every object.

Logging

What if your having difficulty displaying things to screen or don’t want any users to see debug output. With this in mind you can also use a built in logging function, similar to Zend_Log and is essentially a wrapper as well. You can find it defined in app/Mage.php.

/**
    * log facility (??)
    *
    * @param string $message
    * @param integer $level
    * @param string $file
    * @param bool $forceLog
    */
public static function log($message, $level = null, $file = '', $forceLog = false)

And here is an example: Mage::Log($foo);

Which will log the output the contents of $foo to /var/log/system.log by default.

You can also specify your own log file with an extra argument, your custom log file will appear in /var/log/mylogfile.log Mage::log($foo, null, 'mylogfile.log');

You can also use combinations of functions/methods to output the contents of an array for instance: Mage::log(var_dump($fooArray), null, 'mylogfile.log');

Logging MUST be enabled within the admin: Configuration -> Developer -> Log Settings -> Enabled = Yes

XML Configuration

Most of the time, I have have issues with my XML configurations. Since Magento is very configuration based driven, one improper case or underscore can render things useless. Magento doesn’t validate the XML or throw any errors when such are encountered but rather ignored. The best means I’ve found to figure out whats going on is to display the entire XML built from all XML configurations files with.

header("Content-Type: text/xml");
die(Mage::app()->getConfig()->getNode()->asXML());

xDebug

xDebug is probably one of the more well known and most used debugging tools available. If your IDE does support it, I would highly suggest taking the time to get your environments setup so that you can connect and use it. I’m not going to cover the general use and configuration of it, however Classy Llama has a nifty post that helps keep Magento from letting xDebug take over error handling.

Classy Llama’s Enable xDebugs Error Handler

NOTE: It requires modification to the Core files and cannot be extended since the class is final. Make note of your change when doing this, or merely use it on a per need basis and removing it after your done with it. You can also setup your version control to ignore any changes with it. Built-in PHP functions

PHP Methods

If you’re using a bare bones editor without any type of auto complete looking up available class methods can be a pain digging through Magento’s numerous files and folders. To get all available methods from any class you can use var_export, get_class_methods and get_class in combination.

print "<pre>"; var_export(get_class_methods(get_class($this)));

You can also use it in combination with Magento’s getData() to display data with key values intact.

print "<pre>"; var_export(array_keys($this->getData()));

Developer Mode

One last tip I’ve been doing lately is modifying index.php and adding ini_set('display_errors', 1); to the condition checking for the developer mode flag: MAGE_IS_DEVELOPER_MODE. By default the display_errors ini_set is commented out. Here is what my change looks like:

if (isset($_SERVER['MAGE_IS_DEVELOPER_MODE'])) {
    Mage::setIsDeveloperMode(true);
    ini_set('display_errors', 1);
}

Then use .htaccess SetEnv to enable and disable developer mode per environment:

SetEnv MAGE_IS_DEVELOPER_MODE "true"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment