Using debug.log for debugging in WordPress
– WITHOUT notices and warnings….
When developing WordPress sites sometimes it can be necessary to check what is going on behind the scenes without outputting directly to the screen. I use the debug.log file for checking background processes when I do not want or it is not practical to see debug data outputted on the screen itself.
You turn on debug logging in WordPress by editing your wp-config.php with the following code:
define('WP_DEBUG', true); // turn on debugging - turn off on production sites define('WP_DEBUG_LOG', true); // log the debug data to the file wp-content/debug.log define('WP_DEBUG_DISPLAY', false); // do not display debug messages to the screen
Errors and warnings will be logged to the file
wp-content/debug.log. This is helpful, but when in many cases you will be working with websites that use 3rd party plugins and/or a theme that output notices and warnings when you turn on debugging.
In extreme cases a single visit on a page on a local website created hundreds of lines in the debug.log file, making it close to impossible to use for debugging properly.
It is best practice for developers to reduce or completely remove notices and warnings, but this is not always the case.
A simple solution that I read about online (sorry, I don’t remember where I found this tip, so I cannot properly credit the tip):
– Create a .php file and put it in the
wp-content/mu-plugins/ folder. Create the folder if it does not exist.
– You can name the file whatever you want, I call it
debuglogtweaker.php and copy-pasty the following code:
<?php error_reporting(E_ALL & ~( E_DEPRECATED | E_USER_DEPRECATED | E_USER_NOTICE | E_STRICT | E_NOTICE )); ?>
Note: Plugins and .php files in the wp-content/mu-plugins/ runs automatically, read more about this here: codex.wordpress.org/Must_Use_Plugins.
Must-use plugins (a.k.a. mu-plugins) are plugins installed in a special directory inside the content folder and which are automatically enabled on all sites in the installation. Must-use plugins do not show in the default list of plugins on the Plugins page of wp-admin – although they do appear in a special Must-Use section – and cannot be disabled except by removing the plugin file from the must-use directory, which is found in wp-content/mu-plugins by default.
This file will change which details are saved. It’s set to save everything (E_ALL) – EXCEPT the list inside, deprecated, notices, strict warnings, etc.
You will no longer see these errors, and your debug.log file will now contain fatal errors and the notices you output yourself.
You can in your code output details that you want to monitor with the php function error_log(), example:
<?php error_log('---- Output from my function ---'); error_log(print_r($dataarray,true)); ?>
Remember, you should turn off debugging on production sites.