Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for global error handling in PHP?

I used a class that converts errors to exceptions in PHP 5 and it logs the errors to a file and/or emails them to a specified account. Is there a better way to do this? There is something about this I know can be better. I am using set_error_handler.

set_error_handler("exception_error_handler");

My code does what it should in that it logs and emails errors but am I doing the process the best way. Would it be better to log it to a Database - assuming a data connection would be present in an error. What is the industry standard for web sites?

like image 618
Todd Moses Avatar asked Dec 29 '22 01:12

Todd Moses


1 Answers

Your code for dealing with errors must be absolutely bulletproof.

Sometimes it will kick in because of a really obscure reason that you forgot to test for, but you still want it to run when when its struggling through the code version of the apocalypse.

Writing its output to a database creates a huge dependency for you code - and the absence of the database is most likely to be a major cause of problems which would be reported.

Relying on mail is still a dependency, however the most immediate objective in the event of an outage should be to get the system working again - so sending an email is a very effective way of alerting you that you need to fix something.

PHP's file handling facilities do not lend themselves to concurrent access - so although I'd recommend logging any events locally, do not write the files from your code - use the syslog interface. By all means send an email with the relevant details after you've sent it to the syslog.

HTH

C.

like image 100
symcbean Avatar answered Jan 04 '23 17:01

symcbean