I just handcrafted a complex order process which will go online soon. For the first time, I want to be informed about any errors that occur, but I also want them to be logged. This answer suggests to first log them and then mail them to be more secure since mailing might fail. But when I catch the exception, it's not being logged, and when I don't catch it, I can't send an email.
My code:
// convert errors to exceptions
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
try {
// ... some code that might cause errors or throw exceptions
} catch(Exception $e) {
log(...); // how can i make the error being logged here?
mail(...); // afterwards i send an email
throw $e; // i could re-throw it this way, but only after mailing it, cause it kills the process.
}
Sure, I could write my one logging routine, but I guess PHP already does it in a clever way which I'd like to use.
Another thing that would be great is having some control that makes sure that no more then x error emails per hour are sent out.
All in all it seems to be a pretty common setup, but I couldn't find a ready solution. Or is the idea somehow stupid in the hole?