Skip to content

Instantly share code, notes, and snippets.

@ChristianOellers
Last active September 29, 2020 19:38
Show Gist options
  • Save ChristianOellers/0cb47f76b16a3faa8771d270f13e3465 to your computer and use it in GitHub Desktop.
Save ChristianOellers/0cb47f76b16a3faa8771d270f13e3465 to your computer and use it in GitHub Desktop.
Magento E-Commerce CE - Native debugging options
<?php
/**
* A few different ways to do debugging and logging in Magento.
* The output is spare - specific extensions are recommended.
*/
# -------------------------------------------------------------------------------------------------------------------------------- Debugging
// \app\code\core\Mage\Core\functions.php
mageDebugBacktrace(false, true, true);
// Backtrace variant
$result = Varien_Debug::backtrace(true, true, true);
// ... and as plain PHP
$result = array_reverse(debug_backtrace(), true);
// Get variables (check if defined in scope)
$vars = get_defined_vars();
$newVars = $model->getData();
$vars = array_diff(get_defined_vars(), $vars);
# ---------------------------------------------------------------------------------------------------------------------------------- Logging
Zend_Debug::dump($vars);
Mage::log($vars, null, 'var-debug.log', true);
Mage::getModel('core/log_adapter', 'custom.log')
->log(['example', 1, false]);
Magento 1.6 has a Prototype.js debugger with eval() console.
It seems to be unused by the system itself and may be redundant.
Here's how to implement it though.
----------------------------------------------------------------------------------------------------------------------- 1. Extend Layout XML
XML file:
- \app\design\frontend\base\default\layout\page.xml
Required JS:
- \js\prototype\*
Add JavaScripts:
- window.js : Required by debugger (e.g. HTML view)
- window_ext.js : Optional: Cookie for debugger status
- debug.js : Base class (no need to call directly)
- extended_debug.js : Debugger with eval(). Call with: showExtendedDebug()
Example XML:
<action method="addJs"><script>prototype/window.js</script></action>
<action method="addJs"><script>prototype/window_ext.js</script></action>
<action method="addJs"><script>prototype/debug.js</script></action>
<action method="addJs"><script>prototype/extended_debug.js</script></action>
------------------------------------------------------------------------------------------------------------------- 2. Extend Page templates
Open any of the page templates. This is just an example ...
The debugger will appear as drag&drop popup.
<head>
<?php echo $this->getChildHtml('head'); ?>
<link rel="stylesheet" type="text/css" href="js/prototype/windows/themes/default.css">
<link rel="stylesheet" type="text/css" href="js/prototype/windows/themes/debug.css">
</head>
<script>
showExtendedDebug();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment