How To Quickly Debug A Magento 2 Store With Some Easy Steps
This blog covers the actual methods of debugging a Magento 2 store easily.
Debugging is an essential part of every development project or eCommerce store. And every Developer knows how it’s super-necessary to debug Magento 2 store when need arises.
Magento 2 is a newer version of Magento; debugging Magento 2 is slightly different from Magento 1. For a developer, it is crucial to learn about it because Magento’s market share has increased from 9% to 13% in just the last year!
For a breakdown of Magento 1 and Magento 2, read our blog on which Magento version you should choose.
There are multiple methods used to debug code in a Magento 2 Store. In this article, I will show you some of the best Magento debugging methods so you can use the Magento 2 eCommerce store more conveniently.
Why Do You Need to Debug Magento 2 Store?
Debugging is the testing framework; it will help you identify the bugs on your Magento eCommerce store. It will also assist you in resolving bugs/errors or abnormalities in computer programs.
Type of Development Mode in Magento 2 Debugging
Before going through the methods of debugging magento 2, you need to understand the types of development modes. There are three types of development modes: Default, Developer, and Production mode—Let’s discuss each of them one by one:
1- Default mode
If no other mode is enabled, the default mode is literally “the default mode” with no amendments, changins, editing, personalization or additions. This mode allows the following things:
- When you are using default mode, the error is not displayed to the visitors and stored in log files.
- The static files are published to the pub/static record for making them faster.
- The static file is dynamically generated in response to a request.
2- Developer Mode
The developer mode is used for development. It includes the following:
- The static files are published to the pub/static record (as in the default mode). With that, they are always generated from basic templates.
- Users can see the errors. It is displayed in the browser.
- Errors that get in var/reports are more detailed.
3- Production mode
Production mode is one of the best modes. It is fully optimized and suitable for server production. The production mode offers all data that will be loaded from the cache.
This mode allows the following things:
- The errors will not display in the log files
- The static files displayed in the cache only.
If you want to change the developer mode’s production mode, you need to navigate bin/Magento starting with settings deploy:mode: set Developer.
After that, you need to delete the content of the directories that we mention below:
/*Code Start/*
var/cache
var/di
var/generation
var/view_preprocessed
pub/static
/*Code End/*
Note: Do not delete the .htaccess file and the file of pub/static/deployed_version.txt version.
Best Methods of Debugging Magento 2 Store
I have mentioned some of the best methods for you to check out and debug your Magento 2 eCommerce store. Let’s see one by one of each method and make your Magento 2 store faster and wiser.
Xdebug and PhpStorm Combination
Xdebug and PhpStorm, both have a great combination to debug Magento 2. The main benefit of the method is the breakpoints installation process and can assist the developer review and change variables at any time
Magento 2 Developer mode
Magento 2 developer mode shows the errors on your browser screen. If you want to enable this mode, you need to add the following code line in the directory using the console:
php bin/magento deploy:mode:set developer
Enable ‘Template Path Hints
The template path hints method shows the names of all page blocks and classes. To enable the template path, you need to do the following steps:
- Login in to your Magento 2 store
- Next, navigate the store configuration
- Next, go to the Advanced section and click on the Developer
- Next, extend Debug from the menu and set Enabled Template Path Hints for Storefront
- Next, enable Template Path Hints for Admin and Add Block Names to Hints to YES.
Enable Magento 2 Display Errors
Magento 2 does not show the display error on the screen. However, if you want to show this error, you need to rename the local.xml.sample file placed in pub/errors to local.xml.
The content of local.xml.sample:
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config>
<skin>default</skin>
<report>
<!--
"action" can be set to "print" to show exception on screen and "email"
to send exception on specified email
-->
<action>print</action>
<!--
in "subject" you can set subject of email
-->
<subject>Store Debug Information</subject>
<!--
"email_address" admin email address
-->
<email_address></email_address>
<!--
"trash" is handle about trace info
value "leave" is for store on disk
value "delete" is for cleaning
-->
<trash>leave</trash>
</report>
</config>
Magento 2 Logs
This method is hidden from the user and doesn’t require displaying the data on the screen. Furthermore, in Magento 2, there’s the PsrLogLoggerInterface class that provides logging events and output variables to Magento log files.
Following are the public method that you can see:
emergency($message, array $context = array())
alert($message, array $context = array())
critical($message, array $context = array())
error($message, array $context = array())
warning($message, array $context = array())
notice($message, array $context = array())
info($message, array $context = array())
debug($message, array $context = array())
log($level, $message, array $context = array())
Frequently Ask Questions
How to activate error messages in Magento 2?
There are two methods that you can use.
- Changing mode into the “developer.”
- Rename local.xml.sample in pub/error directory to local.xml
What is Xdebug PHP?
Xdebug is using the DBGp debugging protocol. It is a PHP extension that provides debugging capabilities.
What Debugging means?
Debugging is the process you can use and identify your Magento 2 store’s error.
How to Turn On / Off debug log in Magento 2?
By default, debug log is on in default or develop mode, and off in production mode.
Use:
bin/magento setup:config:set --enable-debug-logging=true
or
bin/magento setup:config:set --enable-debug-logging=false
Final Words
Debugging Magento 2 is a skill that will set you apart from most of the developers. In the current era, it is crucial to learn how to debug Magento 2 because customization and personalizations are most important when it comes to eCommerce stores.
I hope this would be sufficient to know how to debug a Magento 2 store with the above-mentioned methods.
Also, if you have experienced one of the methods mentioned above, then share it with us. We highly appreciate your feedback. For any queries, contact us at info@devrims.com.
If you are looking for a secure hosting platform with free migration, check out Devrims Magento Hosting.