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.

1
2
3
4
5
6
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.

1
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.

1
[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.

1
2
3
4
5
6
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.

Category: 

Share:

  • Add news feed
  • Bookmark this on Delicious

Comments

philipnorton42's picture

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 :)

Thank you for the clear info. Have a good day.

Add new comment