zoukankan      html  css  js  c++  java
  • php错误消息捕获

    <?php
    header('Content-type:text/html;charset=UTF-8');
    //function_exists('ini_set') && ini_set('display_errors', 'On');
    error_reporting(E_ALL | E_STRICT);
    register_shutdown_function(array('debug', 'shutdown_handler'));
    
    //function_exists('ini_set') && ini_set('display_errors', 'On');
    set_error_handler(array('debug', 'error_handler')); // 设置错误处理方法
    set_exception_handler(array('debug', 'exception_handler')); // 设置异常处理方法
    
    class debug{
        public static function shutdown_handler() {
            if($e = error_get_last()) {
                ob_clean();
                echo "Shutdown: 
    " . print_r($e);
            }
        }
    
        public static function error_handler($errno, $errstr, $errfile, $errline) {
            defined('E_DEPRECATED') || define('E_DEPRECATED', 8192);
            defined('E_USER_DEPRECATED') || define('E_USER_DEPRECATED', 16384);
    
            $error_type = array(
                E_ERROR                 => '运行错误',
                E_WARNING               => '运行警告',
                E_PARSE                 => '语法错误',
                E_NOTICE                => '运行通知',
                E_CORE_ERROR            => '初始错误',
                E_CORE_WARNING          => '初始警告',
                E_COMPILE_ERROR         => '编译错误',
                E_COMPILE_WARNING       => '编译警告',
                E_USER_ERROR            => '用户定义的错误',
                E_USER_WARNING          => '用户定义的警告',
                E_USER_NOTICE           => '用户定义的通知',
                E_STRICT                => '代码标准建议',
                E_RECOVERABLE_ERROR     => '致命错误',
                E_DEPRECATED            => '代码警告',
                E_USER_DEPRECATED       => '用户定义的代码警告',
            );
    
            $errno_str = isset($error_type[$errno]) ? $error_type[$errno] : '未知错误';
            $s = "[$errno_str] : $errstr";
            if(true) {
                throw new Exception($s);
            }else{
                // 线上模式放宽一些,只记录日志,不中断程序执行
                if(in_array($errno, array(E_NOTICE, E_USER_NOTICE, E_DEPRECATED))) {
                    // log::write($s);
                }else{
                    throw new Exception($s);
                }
            }
        }
    
        public static function exception_handler($e) {
            $trace = $e->getTrace();
            if(!empty($trace) && $trace[0]['function'] == 'error_handler' && $trace[0]['class'] == 'debug') {
                $message = $e->getMessage();
                $file = $trace[0]['args'][2];
                $line = $trace[0]['args'][3];
            }else{
                $message = '[程序异常] : '.$e->getMessage();
                $file = $e->getFile();
                $line = $e->getLine();
            }
            
            var_dump($message);
        }
    }
    
    echo $d;
    echo ed();
    

      

  • 相关阅读:
    构建之法阅读笔记01
    软件工程个人作业01
    第一个PSP0级
    java实现课表的增加
    软件工程概论01
    异常处理
    流与文件课件课后作业1计算容量
    第九周课堂测试
    第八周动手动脑
    JAVA项目中常用的异常知识点总结
  • 原文地址:https://www.cnblogs.com/adtuu/p/4763289.html
Copyright © 2011-2022 走看看