Get all PHP errors/warnings/… that occurred during the current request

后端 未结 5 1645
野性不改
野性不改 2021-02-13 18:07

Setting the directive display_errors to true (while having error_reporting set to E_ALL) prints all errors that occured durin

5条回答
  •  既然无缘
    2021-02-13 18:51

    Fuller example that catches all the errors (exceptions and errors), pumps to a log file and remembers in a static class; at the end you query the static class to get the error.

    I use similar to this for all projects to get full control over all errors.

    function customErrorHandler($errno, $errmsg, $filename, $linenum, $vars) {
        ErrorHandler($errno, $errmsg, $filename, $linenum, $vars, true);
    }
    
    function nullErrorHandler($errno, $errmsg, $filename, $linenum, $vars) {
        ErrorHandler($errno, $errmsg, $filename, $linenum, $vars, false);
    }
    
    function customExceptionHandler($exception) {
        if (is_a($exception, 'exceptionError')) {
            echo $exception->output();
        } else {
            ErrorHandler(E_ERROR, $exception->getMessage() . '(' . $exception->getCode() . ')', $exception->getFile(), $exception->getLine(), null, true);
        }
    }
    
    function nullExceptionHandler($exception) {
        if (is_subclass_of($exception, 'exceptionError')) {
            $exception->output();
        } else {
            ErrorHandler(E_WARNING, $exception->getMessage() . '(' . $exception->getCode() . ')', $exception->getFile(), $exception->getLine(), null, false);
        }
    }
    
    
    function ErrorHandler($errno, $errmsg, $filename, $linenum, $vars, $fatal) {
        $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_DEPRECATED         => 'Deprecated',
                E_USER_ERROR         => 'User Error',
                E_USER_WARNING       => 'User Warning',
                E_USER_NOTICE        => 'User Notice',
                E_USER_DEPRECATED    => 'User Deprecated',
                E_STRICT             => 'Runtime Notice',
                E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'
                );
    
    
        // Pushed into error log
        if (error_reporting() > 0) {
            $message = ': ' . $errmsg . ' in ' . $filename . ' on line ' . $linenum;
            error_log('PHP ' . $errortype[$errno] . $message);
    
            errorLogger::log($message);
    
            if ($fatal) {
                echo $errortype[$errno] . ': ' . $message;
                exit();
            }
        }
    }
    
    
    class errorLogger {
    
        private static $aErrors = array();
    
        // ******************* Timing functions *********************************
    
        public static function log($message) {
            self::$aErrors[] = $message;
        }
    
        public static function getErrors() {
            return self::$aErrors;
        }
    
    }
    

    Usage Example

    // Custom error handler. Can cope with the various call mechanisms
    $old_error_handler = set_error_handler('nullErrorHandler');
    $old_exception_handler = set_exception_handler('nullExceptionHandler');
    
    // Do your stuff
    // *
    // *
    // *
    // *
    // *
    
    $old_error_handler = set_error_handler('customErrorHandler');   // Set to 'nullErrorHandler' to allow it to continue
    $old_exception_handler = set_exception_handler('customExceptionHandler');   // Set to 'nullExceptionHandler' to allow it to continue
    
    // At end
    $errors = errorLogger::getErrors();
    foreach($errors as $errorMessage) {
        echo $errorMessage;
    }
    

提交回复
热议问题