PHP自己的框架自定义错误器set_error_handler和register_shutdown_function(完善篇五)

1、PHP自己的框架实现错误显示和记录日志

 

 

 

 2、运行时错误set_error_handler和致命错误register_shutdown_function KJ.php

    public static function run(){
        //定义常量
        self::_set_const();
        //创建模块目录
        self::_mk_module();
        //加载文件
        self::_import_file();
        self::_set_system();
        set_error_handler(array(__CLASS__,'error'));//运行时错误
        register_shutdown_function(array(__CLASS__,'fatal_error'));//致命错误
        //类自动加载
        spl_autoload_register(array(__CLASS__,'_autoload'));

        //运行框架
        self::_run();

    }

3、错误信息处理KJ.php

    public static function fatal_error(){

        if ($e = error_get_last()) {;
            self::error($e['type'],$e['message'],$e['file'],$e['line']);
        }
    }

    public static function error($errno, $error, $file, $line){
        $err_code="";

        switch ($errno) {
            case E_ERROR:
            case E_PARSE:
            case E_CORE_ERROR:
            case E_COMPILE_ERROR:
            case E_USER_ERROR:

                $msg=$error.$file."第{$line}行";

            errHalt($msg);exit;

            case E_STRICT:
            case E_USER_WARNING:
            case  E_USER_NOTICE:
            default:

                if(config('DEBUG')){
                    $msg=$error.$file."第{$line}行";
                    errLog($msg);
                    include KJ_CORE.'/tpl/notice.tpl';exit;
                }
        }

    }

4、记录处理日志方法function.php

function errHalt($error,$level='ERROR',$type=3,$dest=NULL){
    if(is_array($error)){
        errLog($error['message'],$level,$type,$dest);
    }else{
        errLog($error,$level,$type,$dest);
    }

    $e=array();
    ;
    if(config('DEBUG')){

        if(!is_array($error)){

            $trace=debug_backtrace();

            $e['message']=$error;
            $e['file']=$trace[0]['file'];

            $e['line']=$trace[0]['line'];
            $e['class']=isset($trace[0]['class'])?$trace[0]['class']:"未知";
            $e['function']=isset($trace[0]['function'])?$trace[0]['function']:"未知";
            ob_start();
            debug_print_backtrace();
            $e['trace']=htmlspecialchars(ob_get_clean());

        }else{
            $e=$error;
        }
    }else{
        $e['message']='系统错误';
    }
    include  KJ_CORE.'/tpl/halt.tpl';

    die();
}
function errLog($msg,$level="ERROR",$type=3,$dest=NULL){
        if(is_null($dest)){
            $dest=CACHE.'/errLog/'.date("Ymd").".log";
        }
    if(!is_dir(CACHE.'/errLog')){
        mkdir(CACHE.'/errLog', 777);
   
    }
      error_log("time:".date("Y-m-d H:i:s").
          "{$level}:{$msg}\r\n",$type,$dest);
    }

5、错误显示模板

1、halt.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <title>跳转提示</title>
    <style type="text/css">
        *{ padding: 0; margin: 0; }
        body{ background: #fff; font-family: "Microsoft Yahei","Helvetica Neue",Helvetica,Arial,sans-serif; color: #333; font-size: 16px; }
        .system-message{ padding: 24px 48px; }
        .system-message h1{ font-size: 100px; font-weight: normal; line-height: 120px; margin-bottom: 12px; }
        .system-message .jump{ padding-top: 10px; }
        .system-message .jump a{ color: #333; }
        .system-message .success,.system-message .error{ line-height: 1.8em; font-size: 36px; }
        .system-message .detail{ font-size: 12px; line-height: 20px; margin-top: 12px;  }
    </style>
</head>
<body>
<div class="system-message">
    <h1>:(</h1>
    <p class="error"><?php echo(strip_tags($e['message']));?></p>
    <?php if(config('DEBUG'))?>
    <p class="detail">文件:<?php echo($e['file']);?> 第<?php echo($e['line']);?>行</p>
    <p class="detail">类:<?php echo($e['class']);?>,   方法  <?php echo($e['function']);?></p>

    <p class="detail"><pre><?php echo($e['trace']);?></pre></p>
    <?php ;?>

</div>

</body>
</html>
 2、notice.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <title>跳转提示</title>
    <style type="text/css">
        *{ padding: 0; margin: 0; }
        body{ background: #fff; font-family: "Microsoft Yahei","Helvetica Neue",Helvetica,Arial,sans-serif; color: #333; font-size: 16px; }
        .system-message{ padding: 24px 48px; }
        .system-message h1{ font-size: 100px; font-weight: normal; line-height: 120px; margin-bottom: 12px; }
        .system-message .jump{ padding-top: 10px; }
        .system-message .jump a{ color: #333; }
        .system-message .success,.system-message .error{ line-height: 1.8em; font-size: 36px; }
        .system-message .detail{ font-size: 12px; line-height: 20px; margin-top: 12px;  }
    </style>
</head>
<body>
<div class="system-message">

    <p class="error"><?php echo(strip_tags($error));?></p>
    <?php if(config('DEBUG'))?>
    <p class="detail">文件:<?php echo($file);?> 第<?php echo($line);?></p>

    <?php ;?>

</div>

</body>
</html>

6、错误演示indexCrl.php

1、运行时错误
<?php
class indexCrl extends  CrlBase {
    public function index(){
      $add;
      var_dump($add);exit;
    }

}
2、致命错误
<?php
class indexCrl extends  CrlBase {
    public function index(){
      $add
      var_dump($add);exit;
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_39934453/article/details/132419113