Logging Errors In WordPress

21st June 2011

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. define('WP_DEBUG', true);
  2. if (WP_DEBUG) {
  3. define('WP_DEBUG_LOG', true);
  4. define('WP_DEBUG_DISPLAY', false);
  5. @ini_set('display_errors', 0);
  6. }

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. define('WP_DEBUG', false);
  2. if (WP_DEBUG) {
  3. define('WP_DEBUG_LOG', true);
  4. define('WP_DEBUG_DISPLAY', false);
  5. @ini_set('display_errors', 0);
  6. }

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

Permalink

if(WP_DEBUG)

should really be

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

Cheers!

Carl Helmertz (Tue, 06/21/2011 - 22:05)

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

philipnorton42 (Tue, 06/21/2011 - 22:26)

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

Jan (Thu, 09/08/2011 - 18:56)

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

Muzietto (Wed, 05/09/2012 - 06:20)

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' ); }

Frank (Thu, 09/20/2012 - 07:14)

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 .

http://www.com… (Sat, 06/22/2013 - 13:53)

Add new comment

The content of this field is kept private and will not be shown publicly.