PHP:如何使用set_error_handler()正确处理除通知以外的所有错误? [英] PHP: How to use set_error_handler() to properly deal with all errors except notices?
问题描述
我对如何正确使用set_error_handler()和 php文档并没有真正帮助澄清。
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.
<?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.
}
?>
以上是我当前的设置 set_error_handler('globalErrorHandler',E_USER_WARNING);
,这似乎是错误的,因为它没有涵盖trigger_error()错误。我认为这是因为该参数应该是一个位掩码,而不是一个错误级别,但是我不确定如何设置它以使错误/信息的最大数量有效(通知除外)。我已经看到了使用 E_ALL
的示例,但这实际上直接导致包含全局错误处理程序内容的任何代码对我来说都是错误的。
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.
因此,无论如何,我如何使用set_error_handler,以便可以通过我的自定义错误处理程序处理最大数量的信息(这样,当发生此类问题时,我可以直接获取自动电子邮件,而不必查看日志)
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).
推荐答案
set_error_handler('some_handler',E_ALL & ~E_NOTICE & ~E_USER_NOTICE);
或者,如果您真的想要全部,
Or, if you really want all,
set_error_handler('some_handler',-1 & ~E_NOTICE & ~E_USER_NOTICE);
或者,您可以将其设置为使用所有错误,如果不是,则将其忽略在 error_reporting
(您将其设置为与上一行相同的值,然后 @
运算符也可以工作):
Alternatively, you can just set it to use all errors, and just ignore it if they're not in error_reporting
(which you set to the same value as the above line, also, the @
operator works then):
....
if(!($errno & error_reporting())) return true;
switch($errno){
....
这篇关于PHP:如何使用set_error_handler()正确处理除通知以外的所有错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!