Recommendations for handling global errors in PHP?

I used a class that converts errors to exceptions from PHP 5 and logs errors in a file and / or sends them by email to the specified account. Is there a better way to do this? There is something in it, I know, maybe better. I am using set_error_handler.

set_error_handler("exception_error_handler"); 

My code does what it takes to log errors and emails, but I do the process the best I can. It would be better to register it in the database - provided that the data connection is present in the error. What is the industry standard for websites?

+4
source share
2 answers

Your error correction code should be completely bulletproof.

Sometimes this will be caused due to a really obscure reason why you forgot to check, but you still want it to start when it fought for the code version of the apocalypse.

Writing your output to the database creates a huge dependency for your code, and the lack of a database is likely to be the main cause of the problems that will be reported.

Relying on mail still depends, but the most immediate task in the event of a failure should be to get the system to work again, so sending email is a very effective way of notifying you that something needs to be fixed.

The possibilities of processing PHP files are not amenable to parallel access, therefore, although I would recommend that you log any events locally, do not write files from your code - use the syslog interface. Be sure to send an email with the relevant information after that you sent to syslog.

NTN

FROM.

+6
source

I would not dump this logic into an error handler.

Take a look at this method from Kohana .

 /** * PHP error handler, converts all errors into ErrorExceptions. This handler * respects error_reporting settings. * * @throws ErrorException * @return TRUE */ public static function error_handler($code, $error, $file = NULL, $line = NULL) { if (error_reporting() & $code) { // This error is not suppressed by current error reporting settings // Convert the error into an ErrorException throw new ErrorException($error, $code, 0, $file, $line); } // Do not execute the PHP error handler return TRUE; } 

Clean and do what the method describes. Now you can move the processing to the exception handler or inside the catch block.

+2
source

Source: https://habr.com/ru/post/1301497/


All Articles