How to setup WordPress error log

Wordpress error log
Wordpress error log

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

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true ); 
define('WP_DEBUG_DISPLAY', false);
  • WP_DEBUG to enable debug mode
  • WP_DEBUG_LOG to enable saving messages to debug.log file
  • WP_DEBUG_DISPLAY to put error and warning messages into HTML code, causing it to be displayed on front-end

debug.log file will be then created in the 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 the debug.log file any message you want. You can also put this snippet into functions.php, that will make it easier for you to handle 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, you still see no debug.log file. Of course, problem could be with one of the installed plugins and you 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 the messages directly there.

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.

One comment

Leave a comment

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