error handling - PHP: How to first log an exception and then report by mail? -
i handcrafted complex order process go online soon. first time, want informed errors occur, want them logged. answer suggests first log them , mail them more secure since mailing might fail. when catch exception, it's not being logged, , when don't catch it, can't send email.
my code:
// convert errors exceptions function exception_error_handler($errno, $errstr, $errfile, $errline ) { throw new errorexception($errstr, 0, $errno, $errfile, $errline); } set_error_handler("exception_error_handler"); try { // ... code might cause errors or throw exceptions } catch(exception $e) { log(...); // how can make error being logged here? mail(...); // afterwards send email throw $e; // re-throw way, after mailing it, cause kills process. }
sure, write 1 logging routine, guess php in clever way i'd use.
another thing great having control makes sure no more x error emails per hour sent out.
all in seems pretty common setup, couldn't find ready solution. or idea somehow stupid in hole?
the function error_log() automatically log message web server's error_log file. if prefer, can specify custom log file vs. default error_log file.
so can log simple error message in error_log
this:
error_log("this error message");
or log exception message in /var/log/web_error_log
:
} catch (exception $e) { error_log($e->getmessage(), 3, '/var/log/web_error_log'); }
see http://ca2.php.net/manual/en/function.error-log.php more details on error_log
.
Comments
Post a Comment