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

43 篇文章 1 订阅
23 篇文章 2 订阅
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;
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PHP隔壁老王邻居

啦啦啦啦啦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值