wordpress error logging how-to Print Friendly Version of this pagePrint Get a PDF version of this webpagePDF error_logging_with_wp_config-howto.html — rev 26 May 2014 ➽ back to dir

  I've been wanting to do proper error logging in wordpress for some time,
  but somehow i couldn't wrap my mind around the pieces and get it working.

  Finally got it figured out i think, and got it working — at least the way i want it. 
  So as usual, here is my babbling to you. hope it helps!
  If you have any more insight into this, or tips to share, please let me know.
Here are the settings i use for error logging in wp-config.php, 
    when i want all errors to go to the log:

@ini_set('log_errors','On');
@ini_set('display_errors','Off');
@ini_set('error_reporting', E_ALL );
@ini_set('error_log','/full/path/Logs/tibetsun-error_log');
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', false);
define('WP_DEBUG_DISPLAY', false); 
Here are the gory details! (same lines, with explanation)

// ini_set tells what we want php to do:
@ini_set('log_errors','On');         // do want to get PHP errors.
@ini_set('display_errors','Off');    // don't want PHP to show errors on pages.
@ini_set('error_reporting', E_ALL ); // whatever level we want of PHP errors.
@ini_set('error_log','/full/path/Logs/tibetsun-error_log'); // See below!
// these constants tell what we want wordpress to do:
define('WP_DEBUG', true);            // do want to get messages from WP itself.
define('WP_DEBUG_LOG', false);       // don't want WP to save to default debug.log in wp-content.
define('WP_DEBUG_DISPLAY', false);   // don't want WP to show errors on pages.
And now, much gorier details!

  * php and wordpress errors aren't separate - wordpress is in php, after all!

  * it's all a little tricky, how ini_set and the WP_ constants affect each other.
	  see the resources below for better explanations.

  * When WP_DEBUG is false, that just means that we won't see
    error messages from any wordpress php scripts that have 
    code in them to emit errors.  (that is, the errors are passed to php.)
    We will still always see php errors (like missing quotes, undefined variables, etc).


............................................................................... 
some defaults:

  * php settings trump wordpress settings.

  * php_ini settings in your wp-config.php, trump settings in php.ini or .htaccess.
      ( i think! )
the default error logs:

  * by default apache writes php and wordpress errors to a file named "error_log".

  * by default php will save errors to a file called "error_log",
      in the directory of the script that made the error.

  * by default wordpress by default will save errors to a file called "debug.log",
      in the wp-content directory.

  * on my live server, apparently it is set up to redirect anything
        named "error_log" to "404 not found" if someone tries to access 
        it through the browser.  This is a Good Thing - there's lots of
        nice information in there for hackers to play with.
        But -- /wp-content/debug.log is not redirected (need to find more how this works).

  * You can view your error log through sftp or ssh.
setting up your own error log:

  We can change where php logs errors with @ini_set().

  * you need to make the log file first: just make an empty file.
      - on your local machine you may need to chmod it 777 to be writable.
      - on live server apache usually can write to a 644 file.

  * you can make it "outside" of the web root.
      - advantage: it can't be seen through the browser. 
                   it doesn't clutter up your website files.
      - example:
         Say your web root on the server is /home/sitename/www/
         You can make a folder in your home directory (/home/sitename/),
            and keep the error log there.
         So like:
          @ini_set('error_log','/home/sitename/Logs/sitename-error_log'); 

On live website, i don't want the log to save all errors.
The log will get really big, really fast.
But i like having it there in case of emergency. So i set it up to log
only fatal errors, and then if an emergency comes up i can see them
right away. Then if i need more i can set it to log all errors and
watch the action.

  My full setup for error logging in my wp-config: Click to see
// define location and name of error log file:
if (@$_SERVER['SERVER_NAME'] == DEV) {
  define( 'SITE_ROOT',  '/var/web/sites/path-to-site-on-local' );
  define( 'LOG_DIR',    '/errlog-dev' );
} else {
  define( 'SITE_ROOT',  '/home' );
  define( 'LOG_DIR',    '/errlog-prod' );
}
define( 'ERRLOG_PATH', '/acct-name/Domain/Logs/' . LOG_DIR . '/acct-name-error_log');
// tell php what we want it to do:
@ini_set('log_errors','On');
@ini_set('display_errors','Off');
@ini_set('error_reporting', E_ERROR | E_CORE_ERROR | E_RECOVERABLE_ERROR );  // only want to record fatal errors.
// @ini_set('error_reporting', E_ALL );                 // big problem: want to see everything.
@ini_set('error_log', SITE_ROOT . ERRLOG_PATH);
// tell wordpress what we want it to do:
define('WP_DEBUG', false);         // false - don't want to get all errors from wordpress.
// define('WP_DEBUG', true);       // true - do want to get all errors from wordpress.
define('WP_DEBUG_LOG', false);     // false - don't want to save to default debug.log in wp-content
define('WP_DEBUG_DISPLAY', FALSE); // false - don't want to show errors on pages.
/** More debugging: see all database queries. How to use this is here:
 *    https://codex.wordpress.org/Editing_wp-config.php#Save_queries_for_analysis
 * You don't want to have this TRUE on production -- will have a performance impact on your site.
 */
define( 'SAVEQUERIES', FALSE );
The comments in the file explaining this: Click to see
/**
 * For developers: WordPress debugging mode.
 *
 *   https://codex.wordpress.org/Debugging_in_WordPress
 *   https://codex.wordpress.org/Editing_wp-config.php#Configure_Error_Log
 *   https://wordpress.org/support/topic/how-to-test-php-error-logging
 *
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * WP_DEBUG tells PHP to report more errors, specifically “notices,” since
 *   WordPress normally instructs PHP to only report warnings and fatal errors.
 *   It also will help you to know if you’re using a deprecated function,
 *   deprecated file, or an old API.
 *
 * Place this *before* any include or require statements.
 * The error log file probably needs to be 777 on your local machine,
 *   and can probably be 644 on the live server.
 *   The file needs to exist before apache can write to it.
 *
 *  26 may 2014: set to write only fatal errors.
 *   here is what you want to see all errors, in emergency:
 *      @ini_set('error_reporting', E_ALL );
 *      define('WP_DEBUG', false);         // true - do want to get all errors from wordpress.
 */
how to see the log?

  * well you could open the file in an editor.
      (the latest entries will be at the end of the file.)
      but then you have to open it again after you load a page,
      to see the new errors -- and that gets boring.

  * what gets exciting is:
      $ tail -f error_log
    Now the log will stay open in the terminal, and you can see it change 
      as entries get written to it.
You can use error logging in your own code: 

  error_log( $message );

  and it will write to the error file you set up!

  well well, the excitement never ends.

  See more at http://fuelyourcoding.com/simple-debugging-with-wordpress/
here is a very nice plugin: Error Log Monitor

    * It will show the last lines of your error log in the dashboard.
    * Only admin can see it.
    * Admin can also clear the log, which is very useful for an easy
        way to do that, and save disk space.
    * It can automatically tell where your error log is being written.
  
  Get it at https://wordpress.org/plugins/error-log-monitor/

resources:

  This is the most useful one:
    https://codex.wordpress.org/User:Bcworkz#Error_Display_and_Logging

  These contain The Word from WordPress:
    https://codex.wordpress.org/Editing_wp-config.php#Configure_Error_Log
    https://codex.wordpress.org/Debugging_in_WordPress

  And this is nice:
      http://fuelyourcoding.com/simple-debugging-with-wordpress/
______________________________________________________________________ 
written 16 january 2013