How to setup WordPress error log

Wordpress error log

By adding a few lines of code into wp-confing file, we can enable the WordPress log and see what is going on.

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true ); 
define('WP_DEBUG_DISPLAY', false);

debug.log file will be then created in WP-Content directory after there is an error or warning generated on your page.

Change debug.log file location

By default, it is set by the wp-includes/load.php to be stored inside WP-Content directory:

ini_set( 'error_log', $log_path );

You can easily overwrite it by using the same approach:

ini_set( 'error_log', WP_CONTENT_DIR . 'YOUR_DIRECTORY/YOUR_FILE_NAME' );

Write custom message to WordPress log

By calling the error_log() function, you can write to debug.log file any message you want. You can also put this snippet into functions.php that will make it easier for you by handling arrays:

/*
*
* Write Log Debugging
*
*/

if ( ! function_exists('write_log')) {
	function write_log ( $log )  {
	   if ( is_array( $log ) || is_object( $log ) ) {
		  error_log( print_r( $log, true ) );
	   } else {
		  error_log( $log );
	   }
	}
 }

 write_log('Debug log test');

WordPress error log still not working

After making sure that wp-config file is set, we still see no debug.log file. Of course, problem could be with one of the installed plugins and we can try deactivating all of them. However, far more probable cause is your php.ini settings.

There is a variable called error_log. Usually it is empty, but it could be your case that it has value like ” /home/html/xxx.xom/logs/php.log ” set, which causes that error_log() function sends all log messages directly here.

Therefore check your php.ini by simply calling php_info() or go to your web administration panel and check if you can see this setting here as well.

Solution is then to either change it yourself (if possible) or ask your webhosting support.


February 1, 2020

Leave a comment

Your email address will not be published. Required fields are marked *