Monitoring PHP errors, warnings, and notices

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 protected]"
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: [email protected]
To: [email protected]
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.

Enjoy!

The Social Landscape

I came across this excellent chart at Omniture web site.  While it is mostly aimed at marketing people, it’s still pretty useful for everyone on the ewb to have an overview of which social networks work better for which purposes.

Gmail Labs : it sucks being a minority

The latest announcement at Gmail blog covers some of the experimental features (Gmail Labs) that will graduate the Labs, or, in other words, will become a part of the Gmail service, and some features that will retire, or, in other words, will be completely removed.  While usually changes like this don’t affect myself much, this time I am in the minority.  That is, I am using one of those features that will be retired – Fixed Width Font.

Fixed width font

A lot of my emails have to do with coding and from automated scripts that send me the output.  These make much more sense when displayed with fixed width font.  Vertical alignment, tabulation levels, and simple table-like structures – these all break horribly when viewed with variable width font.  Too bad that’s how I’ll have to look at it now.

Variable width font

Some of that Google stuff is not for you

Google is all over the news recently with more and better applications.  However, not all of those applications are for you, an average Joe.  Consider for example the blog post about their updates to DoubleClick Ad Exchange, that they’ve bought some time ago.

Imagine you’re a major online publisher with a popular global surfing website and an ad sales team. Every second of every day, you have difficult decisions about what ads to show and how to measure their relative performance.

I think you can stop reading immediately after those lines.  Of course, there are plenty of global publishers who are using Google services in one form or the other.  But let’s face it – you are not one of them.

The power of the keyboard

Lifehacker covers an experiment which tried to compare the speed of different input methods.  As you might have guessed, full-size keyboard wins.

Nobody should ever doubt the power of the full-size keyboard (+ touch-typing).  It’s faster than pen and paper, and thus is faster than anything that imitates pen and paper.  It’s faster than downsized keyboard (QWERTY smartphones and iPhones).  It’s even faster than speaking into a speech-to-text application.  And more accurate as well.  And that will remain for some time to come – until brain-to-computer interfaces will go mainstream.