9. Monitor the PHP Error Log File to Quickly Fix Serious Bugs
10. Fix Your Bugs but Never Edit Code on the Production Server
9. Monitor the PHP Error Log File to Quickly Fix Serious Bugs
Error logs are useful to discover bugs that may be causing PHP errors but you cannot spend all day looking at your PHP error log files.
It would be better if you could be notified when a PHP error occurs. That is the purpose of the Log Watcher class. It can monitor a log file and send an email message to you when new lines that are added to the log file.
You can use it for instance from a script started periodically by cron, lets say every 5 minutes, and make it track any new lines added to the PHP error log file.
Here is a simple example of a script to send email messages to a given email address using the Log Watcher class:
$log_watcher=new log_watcher_class;
/*
* Specify the file name of the log file to watch
*/
$log_watcher->log_file_name=’/path/to/php_error_log’;
/*
* Specify who is going to receive the notifications about the new lines
* added to the log file
*/
$log_watcher->watcher_name=’Your name’;
$log_watcher->watcher_email=’your@email.com’;
/*
* Open the log file and point it to the last line that was read or the
* end of the file if this the first time the log is opened
*/
$log_watcher->OpenLog();
/*
* Send the new log file lines, if any, in a message with the specified
* subject
*/
$log_watcher->MailNews(‘PHP error log report’);
/*
* Close the log file and store the last line position in the pointer
* file
*/
$success=$log_watcher->CloseLog();
10. Fix Your Bugs but Never Edit Code on the Production Server
Once you get new alert emails with new PHP errors that are occurring in your production server, you should act promptly and analyze the information to fix any bugs as soon as possible. This will help you prevent that greater harm is caused by the bugs in your site.
Most bugs need to be fixed urgently. However you should always do it adequately.
One thing that you never should do is to access your production server and edit your code directly. The problem with this approach is that your attempt to fix the code may cause even greater harm. Unfortunately I see many less experienced developers doing this. If you do this, stop doing it now.
The right way to do it is to fix and test your code in the development environment. You should only update your code in production when you are certain that the fix really solves the problem and does not cause other problems. You should also pass your updates through the staging server first.
More PHP Defensive Programming Practices
All these practices described above are just part of a broader development approach called defensive programming. It reflects many years of attempts to improve development practices in such way that you prevent yourself from being affected by bad habits that can cause greater harm.
This means that I learned from my own mistakes to make my practices better and generally safer. I have written about this before in articles about PHP defensive programming and surviving PHP site traffic peaks.
Certainly these are not the only defensive programming practices you can apply in PHP. There are certainly more practices that were not covered here.
Source From: http://www.phpclasses.org