CodeIgniter really is turning out to be The Little PHP Framework That Could. I’ve now dived pretty deep into it and still have few complaints; as I’ve said before, it makes all the boring stuff easy, has almost no “magic”, and stays out of the way.
As the application moves towards production, though, I began to miss a few things from Rails – notably, its ExceptionNotifier plugin. ExceptionNotifier will send you an email every time there’s an error on the site, which is really very useful with production applications.
So I investigated alternatives for CodeIgniter. I stumbled across this Stack Overflow post, which basically outlines exactly what I was looking for.
Except it doesn’t work.
Never mind! We can fix that, and the end result is MY_Exceptions.php
:
(You might want to “view raw” on that – there’s some funky syntax-highlighting going on).
This really does work out-of-the-box with CodeIgniter 1.7.x. You just drop it into system/application/library
, call it MY_Exceptions.php
, and it’ll extend the existing Exceptions library. Obviously, you’re going to need to change a lot of the obvious details like email addresses you want things sent to, and the name of the production domain you’ve configured in your app’s config.php
. You also need to make sure the error log level is set to “1” or higher in that config.php
file. But that’s about it; it really does work, and means that in production alone, you’ll get email from your app when a PHP error gets thrown, along with not only the line number and file the error was thrown in, but the URL that the user was accessing to generate the problem.
Not bad for an hour’s work. And, because it’s a Gist, you can either copy and paste, or just clone it straight into your application.
Shane | 23 Jul 2009
This doesn’t seem to be working for me–it blows up in the same way that the stackoverflow example does. When an exception is found I can watch in the logs where the email class is initialized and then everything freezes and a timeout will occur if I wait long enough. If I comment out the ‘send’ command then the pages will load just fine.
Any idea why the ‘send’ method would be hanging the system?
Gregory Kornblum | 29 Jul 2009
Great work! I always prefer that my applications tell me it needs help before my users do :)
Tom | 29 Jul 2009
Sorry to hear that, Shane. Not sure what it might be – what OS are you running your server on? Might it be something to do with the configuration of sendmail (or equivalent) on your box?
Sorry I can’t be more use, yet…