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, xdebug support, profiler, developer mode and enable template patch hints.
In this article we will discuss both debugging tricks and tools.
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.”
Magento2 Error Reporting
Magento2 comes with 3 different modes,
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.
Full details can be found at:
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“/>
<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:
<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
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.
After this add following code for back trace stack
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)]);
Magento2 Functional Log
Magento2 Enable PHP Display Errors
Magento2 Path Hints For Admin and Frontend
1. Enabled Template Path Hints for Storefront
Download and Install
- Memory Consumption
- List of all Database Queries
- Block nesting
- Cache Storage Information
- Session Storage Information
- Enabled / Disabled Modules
- Request / Response Data
- Events / Observers
- 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