Debugging Tricks and Tools in Magento 2

Debugging Tricks and Tools in Magento 2

Debugging, in computer programming and engineering, is a multistep process that involves identifying a problem, isolating the source of the problem, and then either correcting the problem or determining a way to work around it. The final step of debugging is to test the correction or workaround and make sure it works.
There are built in debuging features in Magento 2 like logging, profiler, developer mode and enable template patch hints.
In this article we will discuss both debugging tricks and tools.

Related Articles

Contact Us

1. Enable or Disable Maintenance Mode

Magento uses maintenance mode to disable bootstrapping. It is recommended to put website on maintenance mode while you’re maintaining, upgrading, or re-configuring your site.
Just we need to add a new blank file “.maintenance.flag” under var/ directory site will automatically goes in maintenance mode.
Once maintenance flag is enabled, site will display like below message:
“Service Temporarily Unavailable
The server is temporarily unable to service your request due to maintenance downtime or
capacity problems. Please try again later.”

2. Magento2 Error Reporting

Magento2 comes with 3 different modes,
  • Default
  • Developer
  • Production
By default, Magento runs under “default” mode and that’s why we don’t get the error log at the front-end of the site. It need to be set on Developer mode to generate error log. You need to rename file local.xml.sample to local.xml under pub/errors.

3. Magento2 Sql Log And Stack

Many of the cases we need to log MySQL queries and stack to debug and find the solution. As Magento2 does not provide a default configuration from admin to enable MySQL log, we need to do code changes in Magento2 di.xml file.
The file can be located to root/app/etc/di.xml. There are few changes which need to be done in this file.
1. Change “Quite” to “File”:
Find word “Quiet” and replace with “File”.
Line before change:
<preference for=”Magento\Framework\DB\LoggerInterface” type=”Magento\Framework\DB\Logger\Quiet“/>
After change
<preference for=”Magento\Framework\DB\LoggerInterface” type=”Magento\Framework\DB\Logger\File“/>
2. Add below code at the end of file but before </config> tag:
<type name=”Magento\Framework\DB\Logger\File”>
<argument name=”logAllQueries” xsi:type=”boolean”>true</argument>
<argument name=”debugFile” xsi:type=”string”>debug/query.log</argument>
<argument name=”logQueryTime” xsi:type=”decimal”>0.50</argument>
<argument name=”logCallStack” xsi:type=”boolean”>true</argument>
3. Clean cache

4. Magento 2 Function Stack

Function stack trick to print function stack within program one or more time help to track and reach the solution.
We need to use below class after namespace line within our class.
Use Magento\Framework\Debug;
After this add following code for back trace stack
print_r(Debug::backtrace(true, true));
We can also log back trace in log file; to log back track in file use below code:
$this _logger info(‘Any String’,
[‘data:’=>Debug::backtrace(t → → rue, true)]);

5. Magento2 Functional Log

We can also write to the logs using the zend library within the program.
$writer = new \Zend\Log\Writer\Stream(BP . ‘/var/log/log_file_name.log’);
$this->_logger = new \Zend\Log\Logger();
// String
$this->_logger->info(‘Your text message’);
// Array
$this _logger info(‘Any String’, [‘data:’=>$array]);

Magento2 Enable PHP Display Errors

Magento2 giving the option to Enable / Disable PHP errors on site. We need to change in following file.

6. Magento2 Path Hints For Admin and Frontend

In Magento 1, there was an option to Enable or Diable Path hints from admin configuration.
MyProject ->admin-> Stores-> Configuration-> Advanced ->Developer
Same option is available in same configuration place in Magento2 with additional feature for
Enable or Disable Path hint for Admin

7. Debug Tab:

  • Enabled Template Path Hints for Storefront
  • Enabled Template Path Hints for Admin (New in Magento2)
  • Add Block Names to Hints

8. Debug Tools

Some useful modules are freely available for debugging.

1. Mgt Developer Toolbar

Download and Install
The toolbar shows you all important information for performance optimization and Magento

Main Features

  • Profiler
  • Memory Consumption
  • List of all Database Queries
  • Block nesting
  • Cache Storage Information
  • Session Storage Information
  • Enabled / Disabled Modules
  • Request / Response Data
  • Handles
  • Events / Observers
  • Plugins
  • Preferences
  • PHP-Info

2. Quickdevbar

Download and Install
The structure of this toolbar is extremely simple you just need to add a new block in the
layout to get your tab running.
The toolbar is displayed by default if your web server is on your local development


  • Info: Main information about controller, route, action and store. Search on core config data. Dedicated tab output for local and global phpinfo.
  • Design:List handles called and display layout structure of nested blocks and containers
  • Profile:View current observers, all events dispatched, collections and models loaded, plugins instantiated
  • Queries:Statistics about executed queries and detailed query listing with syntax highlighting of main SQL keywords
  • Logs:Display log files with ability to reset these files
  • Actions:Easily toggle template hints and inline translation and flush cache
  • Help: Show module version and link to github
Other tools that should be used while developing with Magento are :
  • New Relic.
  • IDE like PHP storm
  • ZRAY
Other recommended points to developers:
  • Always keep a document of what all Modules are installed and active.
  • Plugins are disruptive so an idea of Plugins in the system will help track things quickly.
  • Whenever doing development make sure to use Magento 2 logging system

Magento Extensions Built by our In-house Team of Magento

Use our powerful magento extensions built in-house by our team of certified magento developers to take your ecommerce store to the next level.

Related Articles

Contact Us

Apr 2nd, 2020|
This website or its third party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the privacy policy. By tapping on "I accept" you agree to the use of cookies.