There are a number of ways to monitor PHP errors, warnings, and notices. You can have your application code trigger some error handling, you can use PHP built-in methods, you can have some scripts running in the background analyzing logs, etc. While you already probably do some of it, here is something that you’ll find handy.
First of all, don’t log all PHP noise into a single file. You can easy make separate logs for each project. Somewhere at the top of your project, when it only starts loading, add the following configuration settings:
ini_set('error_reporting', E_ALL); ini_set('log_errors', '1'); ini_set('error_log', '/path/to/project/logs/php_errors.log'); ini_set('display_errors', '0');
This will enable logging of all errors, warnings, and notices into a file that you specified. And, at the same time, it will disable the display of all the logs to your visitors (something that you should definitely do for a production server).
One you’ve done that, you’ll notice another problem. If your application is of any considerable size and/or if it uses a lot of third-party code, you’ll get buried in all those warnings and notices. The file will quickly become very large and boring and leave your attention span. Not good. While you can fight the size of the file with a tool like logrotate, the boredom is a more serious problem. The same notices and warnings appear over and over and over. You’ll fix some of them and the others will stay there forever. What you need as a way to have a quick overview of what is broken and what is noisy.
Today I wrote a quick cronjob to do just that. Here it is in all its entirety.
#!/bin/bash # This script parses the project PHP errors logs every hour, creates the summary of all # errors/warnings/notices/etc and emails that summary to the email specified below. EMAIL="email@example.com" SUBJECT="here.com PHP errors summary for the last hour" PHP_ERRORS_FILE="/path/to/project/logs/php_errors.log" # The log starts with timestamp like [01-Mar-2010 12:48:56]. Timestamp + 1 stamp occupy about 24 bytes ONE_HOUR_AGO=`date +'[%d-%b-%Y %H:' -d '1 hour ago'` # We only need that double backslash because date pattern uses square bracket grep "^\\$ONE_HOUR_AGO" $PHP_ERRORS_FILE | cut -b 24- | sort | uniq -c | sort -n -r | mail -s "$SUBJECT" $EMAIL
You can drop this file into /etc/cron.hourly/report_php_errors.sh, change permissions to executable, and wait for the next run of hourly scripts. If you’ve updated the variables inside the script to reflect the correct email address and path to log file, you’ll get an email every hour which will look something like this:
From: firstname.lastname@example.org To: email@example.com Subject: here.com PHP errors summary for the last hour 14 PHP Notice: Use of undefined constant PEAR_LOG_DEBUG - assumed 'PEAR_LOG_DEBUG' in /some/path/to/some/file.php on line 17 12 PHP Notice: Undefined index: is_printed in /path/to/something.php on line 2035 9 PHP Notice: Undefined index: blah in /some/foo/bar.php on line 42 7 PHP Notice: Undefined offset: 1 in /some/verifier/script.php on line 120
The email will not be limited to 3 or 4 lines. It will actually contain each and every individual notice, error, and warning that occurred during the last hour in your project. The list will be sorted by how often each warning occurred, with the most frequent entries at the top.
With this list you can start fixing your most frequently seen problems, and you can also notice weird activity much faster than just checking the log file and hoping to catch it with your own eyes.