Logging Errors In WordPress

To debug WordPress many sites will tell you to add the WP_DEBUG setting on its own to your wp-config.php file, but this can be quite harmful as many server configurations will start showing PHP errors and warning messages on your site pages. Most sites will also log any errors to some form or error log, but this isn't always the best place for them to go.

It is possible to add a set of debug options that will allow WordPress to log any errors or warnings to a file. Open up your wp-config.php file and add the following lines of code above the line that says "/* That's all, stop editing! Happy blogging. */". Any changes to this file should always go above this line so that you can keep track of them.

define('WP_DEBUG', true);
if (WP_DEBUG) {
   define('WP_DEBUG_LOG', true);
   define('WP_DEBUG_DISPLAY', false);
   @ini_set('display_errors', 0);
}

What these options do is create a file called debug.log in your wp-content directory. If your folder permissions are quite restrictive then you might want create this file manually and setting the permissions on the file to 666. If WordPress is able to create it then the permissions are probably fine, although you might have trouble doing anything with it over FTP.

The code presented here is ideal because it only creates/updates the log file when you have debug turned on. Whereas the code presented on the codex page keeps writing to the log regardless.

To test this out I added the following line of code (which produces an error) into the TwentyTen theme on a fresh WordPress install and then loaded the site.

define ('My_CUSTOM_CONSTANT', array());

Taking a look in the wp-content/debug.log file reveals that the error message that this code produces is now stored in this file.

[20-Jun-2011 17:06:56] PHP Warning:  Constants may only evaluate to scalar values in /var/www/wordpress/wp-content/themes/twentyten/index.php on line 3

Remember to turn debugging off when you have finished your investigation. This can be done by either removing the code entirely or by setting the value of WP_DEBUG to false, preventing the rest of the debugging code from running.

define('WP_DEBUG', false);

if (WP_DEBUG) {
   define('WP_DEBUG_LOG', true);
   define('WP_DEBUG_DISPLAY', false);
   @ini_set('display_errors', 0);
}

There are some further debug flags to help debugging JavaScript and CSS. See http://codex.wordpress.org/Editing_wp-config.php for more information.

Thanks to Mike Little for letting me know about these options.

Comments

if(WP_DEBUG)

should really be

if(defined('WP_DEBUG') && WP_DEBUG)

Cheers!

Permalink

Yes and no. I did originally look at the code and think that. But the WP_DEBUG definition should always be defined as either true or false. If it isn't present then the rest of the code should also be missing as it is meant for debug purposes only.

Still, it's good practice to always test for the existence of the constants before using them. Especially in this case as if you remove the WP_DEBUG clause the if statement will always equate to true :)

Name
Philip Norton
Permalink

I wished to have known this solution some hours ago as I had an error message only shown for a second or so.

Greats from Germany
Jan

Permalink
Thank you for the clear info. Have a good day.
Permalink
I use the follow variant to also include the debug of JS and write custom log files, use it in my Starter for dev with WP.
// For Debug purpose
define( 'WP_DEBUG', TRUE );
if ( WP_DEBUG ) {
	define( 'WP_DEBUG_LOG', TRUE ); // leave teh debug file in WP_CONTENT_DIR . '/debug.log'
	define( 'WP_DEBUG_DISPLAY', TRUE );
	
	// Debug JS
	// When this is defined and set to TRUE the non-minified versions of the Javascripts will be used.
	define( 'SCRIPT_DEBUG', TRUE );
	
	@ini_set( 'log_errors', 'On' );
	@ini_set( 'display_errors', 'On' );
	@ini_set( 'error_log', $_SERVER['DOCUMENT_ROOT'] . '/logs/php_errors.log' );
}
Permalink
Hello my friend! I wish to say that this post is awesome, nice written and include approximately all important infos. I would like to peer extra posts like this .
Permalink

Thank you very much for the invitation :). Best wishes. PS: How are you? I am from France :)

Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
11 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.