错误处理程序函数停止错误日志记录


Error handler function stops error logging

我在脚本的前几行中有这个简单的代码,在我添加错误处理程序函数之前,它在记录错误方面效果非常好。我的错误处理功能做得很好,我没有发现它有什么问题。

ini_set("log_errors" , "1");
ini_set("error_log" , $_SERVER['DOCUMENT_ROOT']."/logs/Errors.log.txt");

这个东西是 php 的默认工作,一旦你开始错误处理,错误日志记录就会停止吗?

如果是,我该如何克服它?

我的错误处理程序函数可能会有所帮助。

function userErrorHandler($errno, $errstr, $errfile = '', $errline = 0, $errcontext = array())
{
    // Check if the error code is not included in error_reporting
    if (!(error_reporting() & $errno))
    {
        return;
    }
    // Restore default handlers to prevent errors in errors
    restore_error_handler();
    if (function_exists('restore_exception_handler'))
    {
        restore_exception_handler();
    }
    // Load error page
    require('_errors/error.php');
    exit();
}
set_error_handler('userErrorHandler');

当您开始使用 set_error_handler() 时,内置的错误处理将不会发生,可能除了解析错误(但前提是error_log是在 PHP 脚本本身之外定义的,例如 .htaccess )。

与PHP回显的默认错误也是如此。

更新

当上一个错误处理程序是内置错误处理程序时,set_error_handler()返回NULL返回值,因此无法使用您自己的函数收到的参数调用它。

好的。因此,它会覆盖默认处理程序并且错误日志记录停止,也没有办法克服这一点。但是这里还可以做其他事情,并产生与预期相同的输出。

function userErrorHandler($errno, $errstr, $errfile = '', $errline = 0, $errcontext = array())
{
    // Getting error type
    $errorType = array (
            E_ERROR            => 'ERROR',
            E_WARNING        => 'WARNING',
            E_PARSE          => 'PARSING ERROR',
            E_NOTICE         => 'NOTICE',
            E_CORE_ERROR     => 'CORE ERROR',
            E_CORE_WARNING   => 'CORE WARNING',
            E_COMPILE_ERROR  => 'COMPILE ERROR',
            E_COMPILE_WARNING => 'COMPILE WARNING',
            E_USER_ERROR     => 'USER ERROR',
            E_USER_WARNING   => 'USER WARNING',
            E_USER_NOTICE    => 'USER NOTICE',
            E_STRICT         => 'STRICT NOTICE',
            E_RECOVERABLE_ERROR  => 'RECOVERABLE ERROR'
            );
    if (array_key_exists($errno, $errorType)) {
        $err = $errorType[$errno];
    } else {
        $err = 'CAUGHT EXCEPTION';
    }
    // Logging error to a certain file
    $file           = ini_get('error_log');
    $error_string   = "[" . date("d-M-Y H:i:s", $_SERVER['REQUEST_TIME']) . '] PHP ' . $err . '::' . $errstr . " in " . $_SERVER['SCRIPT_FILENAME'] . " on line " . $errline . "'r'n";
    error_log($error_string, 3, $file);
    // Check if the error code is not included in error_reporting
    if (!(error_reporting() & $errno))
    {
        return;
    }
    // Restore default handlers to prevent errors in errors
    restore_error_handler();
    if (function_exists('restore_exception_handler'))
    {
        restore_exception_handler();
    }
    // Load error page
    require('_errors/error.php');
    exit();
}
set_error_handler('userErrorHandler');

我想做的一样:)

在此处阅读完整的工作:http://blog.abhishekg.com/2012/06/error-handling-in-php-with-error-logger-working/