Enable Drupal 8 debugging mode and error reporting for local development

Enable Drupal 8 debugging mode and error reporting for local development

If you are just starting with Drupal 8 then one of the most important things you should know is to enable the debug mode and error reporting on Drupal 8 to show errors for better Drupal 8 debugging.

This is really important for backend as well as frontend developer to see the full error on your screen while you are working because you might not know what is the exact issue with the site with just a generic error statement.

This is not going to help :(

The website encountered an unexpected error. Please try again later.


Enable Drupal 8 backend debugging

In Drupal 8 there is a way to backtrace the error and displaying it in the message area. You can put the following code in your settings.local.php file which you can create by copying sites/example.settings.local.php and rename it to sites/default/settings.local.php.

$config['system.logging']['error_level'] = 'verbose';

This makes the development easier on local as it will show the backtrace of the error which you can debug now.

For local Drupal 8 development, you should also enable error reporting, display errors, and display startup error to help you further debugging and fixing major runtime errors.

Enable Twig debugging in Drupal 8 for frontend guys

If you are a front-end developer and work around twig files to beautify the Drupal 8 site then enabling the twig to debug configuration is a must. You can enable this by coping sites/default/services.yml file and renaming the new file to development.services.yml. Once you have the development.services.yml file you can just add a new twig debug parameter to it to enable the twig debugging and see the error information.

        debug: true

By enabling the twig config you will be able to find which HTML markup is coming from which twig template file in the backend. With this Drupal 8 debug trick you will also see what are the other twig template suggestions which will override the current template. It will also let you know the theme in which the twig template file is located so that you don't start editing the wrong twig file.

Twig debugging in Drupal 8

One more amazing thing Twig debugging does for frontend, as well as backend developers, is to print a variable in a structured format in the frontend by using a simple line of code in the Twig file.

{{ dump(my_variable) }}


Drupal 8 local development bonus tips

Whenever you are starting working and project in Drupal 8 make sure to set these settings for your local development environment to make Drupal 8 debugging easy.

Disable Twig caching for Drupal 8 in development.services.yml file you created earlier.

        debug: true
        auto_reload: true
        cache: false

Disable CSS and JS aggregation on the local environment in Drupal 8 by uncommenting the below lines to settings.local.php.

$config['system.performance']['css']['preprocess'] = FALSE;
$config['system.performance']['js']['preprocess'] = FALSE;

Disable Drupal 8 render cache and dynamic page caching by uncommenting the below lines to settings.local.php.

$settings['cache']['bins']['render'] = 'cache.backend.null';
$settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';

If you are using Drupal version greater than 8.4 then do add the following line in the settings.local.php file.

$settings['cache']['bins']['page'] = 'cache.backend.null';

In the settings.local.php file uncomment the following line to enable the null cache service in Drupal 8.

$settings['container_yamls'][] = DRUPAL_ROOT . '/sites/development.services.yml';


Enable Drupal 8 debugging with Drupal Console

If everything mentioned earlier is too much for you and you got access to Drupal Console then a single one-liner command can do the trick for you.

drupal site:mode dev

This Drupal console command will make changes in Drupal 8 configurations such as system.performance, views.settings, and system.logging to enable debugging. Command also makes changes to sites/default/services.yml file related to twig.config for frontend debugging.

And once you are done with debugging Drupal 8 you can revert all configurations back by running another command.

drupal site:mode prod

And the good part is this also rebuilds the cache at the end.

NOTE: Make sure to clear the cache on your Drupal 8 site after all the settings you have done for the local environment development to display debugging information.

WARNING: Make sure to not enable all these setting on the production site, unless you need to ;)