Like the blog? Get the book »

3 Ways to Monitor PHP Errors

3 Ways to Monitor PHP Errors

Close monitoring of your site’s PHP errors is crucial to operating a healthy, secure, and well-performing website. When left undetected, PHP errors can reduce performance, waste bandwidth, and leave your site vulnerable to malicious attack. PHP errors usually occur unpredictably and spontaneously, and may be triggered by even the slightest changes to your server configuration, database setup, or WordPress files. Even if your site appears to working properly on the surface, it may in fact be suffering from undetected PHP errors that should be fixed as soon as possible.

Monitoring PHP errors is something that all responsible WordPress administrators should be doing. In this DiW article, we’ll show you three easy ways to monitor PHP errors for WordPress. The first method is exclusive to WordPress, and the second two methods work great for any website.

Method 1: Error Logging via the WordPress configuration file

Perhaps the easiest way to implement PHP error-logging for your WordPress-powered site is to add a few simple lines of code to your wp-config.php file. The WordPress wp-config.php file may be used to specify various PHP initiation settings to modify the functionality of your PHP installation. In this method, we will take advantage of this feature by implementing basic error monitoring for your site. Here’s how to do it:

Step 1: Create a log file

Create an empty file called “php-errors.log”. This file will serve as your site’s PHP error log. Your server will need write access to this file, so make sure to set the appropriate permissions. This file may be placed in any directory, but placing it above the web-accessible root directory of your site is advisable for security reasons. Once this file is created, writable, and in place, take note of its absolute directory path and continue to the final step.

Step 2: Add the magic code

Next, open your site’s wp-config.php file (located in the root WordPress directory) and place the following code immediately above the line that says, “That's all, stop editing! Happy blogging.”:

// log php errors
@ini_set('log_errors','On'); // enable or disable php error logging (use 'On' or 'Off')
@ini_set('display_errors','Off'); // enable or disable public display of errors (use 'On' or 'Off')
@ini_set('error_log','/home/path/logs/php-errors.log'); // path to server-writable log file

Once in place, edit the third line with the absolute directory path to the php-errors.log file created in the first step. Upload to your server and call it done. All PHP errors will now be logged to your php-errors.log file, thereby enabling you to monitor and resolve errors as quickly as possible.

The other two directives in this tasty little snippet enable you to log and display PHP errors at your will. The current configuration is ideal for production sites, but you may want to enable PHP error display for development purposes. See the code comments for more information on changing these settings.

Cool tips

A couple of notes regarding this method.. First, if you place the following definition to your wp-config.php file, all WordPress debug errors will also be written to your php-errors.log file:

define('WP_DEBUG', true); // enable debugging mode

Also note that the code used for this example demonstrates a very basic implementation. You may add any number of additional @ini_set() directives to enhance and customize your error-logging system as needed.

Method 2: Error Logging via the php.ini file

If you have access to your site’s php.ini file, or if you are able to implement “per-directory” initiation files, this method will enable you to monitor PHP errors for any website, not just those powered by WordPress. The process is very similar to the previous method, and requires the following two steps:

Step 1: Create a log file

Create an empty file called “php-errors.log”. This file will serve as your site’s PHP error log. Your server will need write access to this file, so make sure to set the appropriate permissions. This file may be placed in any directory, but placing it above the web-accessible root directory of your site is advisable for security reasons. Once this file is created, writable, and in place, take note of its absolute directory path and continue to the final step.

Step 2: Add the magic code

Next, open your site’s php.ini file and add the following code:

;;; log php errors
display_startup_errors = false
display_errors = false
html_errors = false
log_errors = true
track_errors = true
error_log = /home/path/logs/php-errors.log
error_reporting = E_ALL | E_STRICT
log_errors_max_len = 0

As before, this code is configured for optimal error-handling for production servers. The only thing that you need to edit is the absolute directory path for your log file. During site development, you may want to change some of the variables to enable error display, startup errors, repeat errors, and so forth. For more information on these directives, and for a more in-depth guide to logging errors with PHP, check out my article at Perishable Press, Advanced PHP Error Handling via PHP.

Method 3: Error Logging via the HTAccess file

Last but not least, let’s look at how to enable PHP error-logging via HTAccess. This method works great if HTAccess files are enabled on your site, and will enable you to log PHP errors on any site, not just those powered by WordPress. The process is very similar to the previous method, and requires the following two steps:

Step 1: Create a log file

Create an empty file called “php-errors.log”. This file will serve as your site’s PHP error log. Your server will need write access to this file, so make sure to set the appropriate permissions. This file may be placed in any directory, but placing it above the web-accessible root directory of your site is advisable for security reasons. Once this file is created, writable, and in place, take note of its absolute directory path and continue to the final step.

Step 2: Add the magic code

Next, open your site’s root .htaccess file and add the following code:

# log php errors
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
php_flag  log_errors on
php_value error_log /home/path/logs/php-errors.log

As before, this code is configured for optimal error-handling for production servers. The only thing that you need to edit is the absolute directory path for your log file. During site development, you may want to change some of the variables to enable error display, startup errors, repeat errors, and so forth. For more information on these directives, and for a more in-depth guide to logging PHP errors via HTAccess, check out my article at Perishable Press, Advanced PHP Error Handling via htaccess.

Troubleshooting Tips

To determine the absolute path of your log file, upload a PHP with the following code to the same directory and open it in a browser:

<h3><?php echo $_SERVER['DOCUMENT_ROOT']; ?></h3>

Depending on your server configuration, you may need to use method #1 or #2 if you are running PHP5. Certain dual-PHP setups and other configurations may require any php_value directives to be placed in a php.ini file instead of htaccess.

An easy way to test that your error-logging system is working is to trigger a few basic PHP errors. To do so, create a PHP file and add something that will trigger an error, for example:

<?php echo "error" ?>

Lastly, if you are triggering errors but nothing is being written to your log file, triple-check that the file is writable by the server. Depending on your particular server configuration, you may need to increase the permissions level of the file. If you have to run with a setting of “777” (full permissions), definitely make sure that the log file is placed above the web-accessible root directory of your site.

Wrap it up then

With one of these three PHP error-logging techniques, you have everything you need to implement easy, automatic error-logging for your website. Everything happens quietly behind the scenes, so remember to keep an eye on things by checking your log file periodically. Doing so will enable you to ensure a well-performing site and the best possible experience for your visitors.

15 responses

  1. Method 1 is not exclusive for WordPress. You can use it with your own php scripts. See ini_set function in PHP manual.

  2. Would be cool if the log would be automatically emailed once a week.

    • Lol – would also be cool to get breakfast in bed and 10 weeks of paid vacation every year, Andy! ;)

      • I honestly just laughed out loud.

      • C’mon guys :) It’s not that far… I get my statistics mailed me every week I know wouldn’t check them so often.

      • its not too hard to set up a cron job to run a php script that would email the contents of the error log to you. and it wouldnt be that much further to also filter out the errors by server log date so you only got the results relevant to that day/week/whatever time period you need. combine these all together and you have a great way to make sure your website is running smoothly

      • would love to see an actual tutorial on this.

  3. security locks

    Great content, very helpfull. The web needs more great sites like this.

  4. THANKS! You guys do a great blog, and have some great contests. Keep up the good work.

  5. Thanks for sharing this coll tip.

  6. Thanks for these tips Jeff. As I got error logging set up on my sites, I decided to knock out a quick way to keep an eye on them: a dashboard widget. It’s pretty rough, and I’ve not found time to wrap it up into a plugin. Just code for a custom theme’s functions.php.

    It has some kinks, like simplistically treating line breaks as delimiters for the errors (I’ve had repeated SQL syntax errors where the line breaks in the reported SQL result in more “errors” than there actually are). But I’m finding it a useful addition to the above techniques. If anyone develops it into (or comes up with) something better – a plugin, an email option, etc. – that’d be great.

    • Looks awesome, Steve. I am a huge fan of logging errors and other data, so this is a real treat. During my next excursion into PHP error-logging, I will be implementing and testing your script. Hopefully I will be able to contribute something useful, perhaps even transform it into plugin. I am sure that it would benefit many WordPress users. Thanks for sharing! :)

  7. I tried the first method, I straight cut and pasted in the code. When I tried to log into the back-end I got a 404. I went back and undid what I had changed and still no luck. Any idea why?

Comments are closed for this post. Contact us with any critical information.
© 2009–2024 Digging Into WordPress Powered by WordPress Monzilla Media shapeSpace