TAG | errors
In my previous post I wrote about few ideas how to capture PHP fatal errors. After some more research I found a few more elegant solutions.
You can wrap all your site with ob_start which accepts a call back function as a parameter which it calls when the content is flushed to the browser. You can use that call back function to preg match for fatal errors.
Another solution is the one I like the most. In my previous post I wrote about a possibility to examine the PHP errors log. Well I found that there are log watch tools to do that easily.
With something like this:
# swatch –daemon -c /root/php_fatal.conf –tail-file=/var/log/apache2/error.log
You can easily track php errors and send them to yourself over the email. And the most exciting detail about this tool is that it won’t spam you with thousands of emails with the same error.
Did you know that PHP custom made error handlers cannot handle Fatal errors or Parse errors. Those are the errors you would like to watch out for most! It’s not some warning or a notice that pops out and makes your visitor scratch his head. It’s an error that makes your visitor leave and sometimes never come back.
So now that we know that those errors are important to catch and there is no easy way to do it what should we do? Well actually there are some ways around to capture those kind of errors.
First way is kind of extreme, crazy and fun.. In php.ini you can find few options:
; String to output before an error message.
;error_prepend_string = "html code"
; String to output after an error message.
;error_append_string = "html code"
Other idea is to make use of a php.ini option error_log = filename. You can log all your php errors to a file. And you can write a cron script that would keep an eye on your file and watch for fatal errors. If it detects one it could easily report about it by sending an email or sending an SMS to your boss that he is losing customers! The cron script could also just empty the file when it gets too big.
I have read some rumors that fatal error handling will be available in PHP6 so there might one day be a proper way to do this. Though it may be that there are other existing solutions.