Php error log monitoring

Recently I've been working on a Wordpress project for a client that involves editing lots of files in lots of different places (think functions.php with various includes - some of which include further files, and several plugins).

There is a lot of stuff going on, and naturally errors occur for various reasons.
What I have been doing is simply logging any errors to a file in the same folder (or occasionally to files in an errors folder, depending on my mood when I was writing the code, I suppose).

When anything bad happened to the site, either:

  • the client would notify me and I would check all the logs - starting wherever I would decide was the most likely place for the problem to have occurred - or:
  • I'd be doing some other work on the site and notice the logs filling up unexpectedly.

Either way, I was always really a bit behind the times, as errors would often occur long before the client or I noticed, potentially leaving parts of the site completely unusable to customers for long periods.

So I decided I need to at the very least be notified (automatically) within an hour of any errors occurring. I searched around for something, preferable nice and simple - and came across the following:

James Caws' 'Automatically monitor and email PHP error log content' script

This fits my criteria very nicely. It:

  • Is written in PHP
  • Uses PHP's mail() function (or is easily modified to use it)
  • Is really simple
  • Is very well commented
  • Requires no unusual dependencies
  • Works straight out of the box
  • Looks like it'll be easy to modify and extend

(I didn't actually require most of these things, but they're very nice to have.)

All I had to do was comment out the lines which weren't required for using mail(), change the values of a few variables, and that was it! It worked as soon as I ran it.

I also modified one line as suggested by James in the comments, to categorise the archived logs into month/year files. (Not required if you don't mind having just a single archive file.)

I set up a cron job to run it every hour, and now I'm notified via email of any PHP errors within the hour of them occurring.

Note: I have little doubt that while this is keeping me happy for now, I don't think I'll be satisfied with it for long. I imagine at the very least I'll want to add some sort of parsing to determine the urgency of the error etc. I don't want to be notified about every little thing..

You can get the original code from James' website, or grab my version, which is currently the same as the original but set up to use mail() and monthly log archiving. The code that's not required is simply commented out.

Find me on Twitter!