Thursday, April 16, 2015

Enable Magento Debug Mode and the Magento Profiler

There are a number of ways to debug in Magento, so I’m going to go through each of the methods that you can use to explore deep inside the core.


Logging Magento Database Queries


It is possible to log every SQL query that Magento calls. To do this we’ll need to edit the database query code to enable the debugging logs. If you’re up to the task, locate and open this file:


lib/Varien/Db/Adapter/Pdo/Mysql.php


Once you’ve located the file you’ll see the following options. Enable the $_debug member to TRUE and save the file.


/**

* Write SQL debug data to file

*

* @var bool

*/

protected $_debug = false;

/**

* Minimum query duration time to be logged

*

* @var unknown_type

*/

protected $_logQueryTime = 0.05;

/**

* Log all queries (ignored minimum query duration time)

*

* @var bool

*/

protected $_logAllQueries = false;

/**

* Add to log call stack data (backtrace)

*

* @var bool

*/

protected $_logCallStack = false;

/**

* Path to SQL debug data log

*

* @var string

*/

protected $_debugFile = "var/debug/sql.txt";


Magento Collection SQL Queries


Magento Collections are responsible for querying the database, filtering the results and joining the extra data tables. If you’re working with a Magento collection you’ll be able to print the sql statement as follows.


echo $query->printLogQuery(true);


Magento Template Path Hints and Block Names


If you’re trying to figure out how to customize the Magento template files, then you’ll want to learn about template path hints. By enabling these you’ll be able to see exactly what template files that Magento is using to create the current view. Follow this navigation to the setting:


Admin (site) > System (nav) > Configuration (nav) > Developer (sidebar nav) > Debug (accordion)


If you cannot see the template path hints or the Magento block name hints, then you’ll need to change the Current Configuration Scope. The Configuration Scope can be changed by selecting your specific store from the dropdown, just above the left sidebar.


Magento Log Files


There are two default log files that Magento will create for you. Exceptions and debug logging, although the debug logs in my opinion are not that thorough. Once enabled, you can also begin creating your own debug logs. Here’s how you enable Magento Exception and Magento Logs.


Admin (site) > System (nav) > Configuration (nav) > Developer (sidebar nav) > Log Settings (accordion)


Debugging Production Sites


If you’re attempting to debug a production website, you’ll want to limit who sees the debugging actions. For this we can use the Magento Developer Client Restrictions. To use this we’ll need to locate our public facing ip address and enter it into the Allowed IPs setting. To get there you’ll need to follow this breadcrumb:


Admin (site) > System (nav) > Configuration (nav) > Developer (sidebar nav) >

Developer Client Restrictions (accordion)


Enable The Magento Profiler


The Magento Profiler is the ultimate debugging system in Magento. This profiler will provide you with a stack trace and sql captures for debugging. To enable the Magento Profiler you’ll first need to enable it within the Magento Developer Settings. Follow this breadcrumb:


Admin (site) > System (nav) > Configuration (nav) > Developer (sidebar nav) > Debug (accordion)


Once it’s enabled in the administration, you’ll need to enable it in the code. The Magento Team has left you all of the pieces within your index.php file. All you need to do is uncomment each of the following items within your index.php file. If one of the lines does not exist, go ahead and create it.


Varien_Profiler::enable();

Mage::setIsDeveloperMode(true);

ini_set("display_errors", 1);


 



Enable Magento Debug Mode and the Magento Profiler

No comments:

Post a Comment