I'm confused about how to use set_error_handler() properly, and the php documentation isn't really helping to clarify.
I want it to email me as many errors as possible, with the exception of notices.
I have the following code
<?php
if (TRAP_ERRORS) {
// True on production, false in development, where errors are just echoed out.
set_exception_handler('globalExceptionHandler');
set_error_handler('globalErrorHandler', E_USER_WARNING);
}
function globalExceptionHandler($e) {
//log and email stuff here
}
function globalErrorHandler($errno, $errstr, $errfile, $errline) {
switch ($errno) {
case E_NOTICE:
case E_USER_NOTICE:
$errors = "Notice";
break;
case E_WARNING:
case E_USER_WARNING:
$errors = "Warning";
break;
case E_ERROR:
case E_USER_ERROR:
$errors = "Fatal Error";
break;
default:
$errors = "Unknown Error";
break;
}
error_log(sprintf("PHP %s: %s in %s on line %d", $errors, $errstr, $errfile, $errline));
$msg = "ERROR: [$errno] $errstr\r\n".
"$errors on line $errline in file $errfile\r\n";
sendErrorEmail($msg);
showErrorPage();
exit(1);
}
function sendErrorEmail($p_errorMsg) {
// Parse and sent out the error email...
}
function showErrorPage() {
// Redirect to an error page.
}
?>
Above is my current setting set_error_handler('globalErrorHandler', E_USER_WARNING);
, which seems to be wrong in that it doesn't cover trigger_error() errors. I believe that is because the argument is supposed to be a bitmask instead of just a single error level, but I am not sure how to set it to work for the maximum number of errors/information (except notices). I've seen examples that use E_ALL
, but that actually directly causes any code that includes the global error handler stuff to error for me.
So anyway, how do I use set_error_handler so that the maximum amount of information can be handled by my custom error handler (so that I can get automatic emails directly when such problems occur, instead of having to review the logs later).