ChromePHP – Easy Way to Debug Magento

Debugging classes

Let’s say we have to fix a frontend element that displays incorrect data – data, which don’t meet our expectations. First, we’d like to know which data are actually fetched to the displayed block. We can see them using PHP var_dump() function. If we have Xdebug installed on the server, this function will enable us to list the whole data content of a given class:

php
1 var_dump($block->getData()); die;

However, if we don’t want to display anything in the view, or we need the logged information about the data content of this class, or we’re debugging the observer’s class, we should use the built-in Magento method: Mage::log();

php
1 Mage::log($block->getData(), null, 'data.log');

This line of the code will save the data from the class in the “data.log” file that by default can be found in the /var/log folder. The prototype for the logging method is the following:

php
1 public static function log($message, $level = null, $file = '', $forceLog = false)

What’s interesting is the $forceLog parameter, which allows you to force logging in even when the functionality is disabled in the backend.

In order to draw the correct conclusions, we’ll most likely want to initiate the fragment of the code responsible for logging in more than once. Therefore, we need to use the following command in the terminal, in the folder containing log files:

bash
1 tail -f data.log

This lets us track all the changes in our file on a regular basis. These are the basic techniques, which aren’t flawless. We either have to break the view or use an additional terminal window, which displays the information on a current basis. All of that can be improved a little. If we don't necessarily need the log history, there is a tool that will enable us to easily display the information from the PHP file in the web browser console in an analogical way the console.log function does for JavaScript.

Chrome Logger – plugin

Chrome Logger is designed only for Chrome (an analogical version for Firefox is called FirePHP) and can be found here:

We need to install this plugin in the web browser.

 ChromePhp

We also need a special PHP class, which can be downloaded here:

http://github.com/ccampbell/chromephp

We have to place this PHP file in the include path of the PHP files, which we can configure in the php.ini file or in the settings of the PHP IDE that we’re using.

 Using ChromePhp

After adding the ChromePhp.php file to the include paths we can apply the static methods of this class:

html
1 ChromePhp::log($block->getData()); ChromePhp::info($block->getData()); ChromePhp::warn('There is something wrong.. i think'); ChromePhp::error('Leave the ship! We are sinking!');

Each static method enables us to generate a different kind of notification – they differ mainly in their representation of the logged message, which can be seen on the screenshot below:

All the notifications were displayed in the web browser console, which significantly facilitates the work and enables logging important events or information conveniently without the necessity to handle additional terminal windows or breaking the views. We used $this instead of $block in the 1column.phtml view only to demonstrate what a great debugging tool ChromePhp is.

You can learn more on this extension under the following link:

https://github.com/ccampbell/chromephp

Enjoy!