1. 程式人生 > >[php] 錯誤接管類

[php] 錯誤接管類

repo -m struct erro 參數 註冊 異常信息 err 錯誤處理

自己弄的一個錯誤接管類:

<?php

//----------------------------------
// Leephp 錯誤接管類
// 2017-07-06
// PengchongLee
//----------------------------------

 // error_reporting — 設置應該報告何種 PHP 錯誤
 error_reporting(0);
 
class Erro
{
    public function __construct()
    {
        $this->iserr();
    }
    public function iserr()
    {    
        
// set_exception_handler — 設置用戶自定義的異常處理函數 set_exception_handler([$this,‘ex‘]); // set_error_handler — 設置用戶自定義的錯誤處理函數 set_error_handler([$this,‘err‘]); // register_shutdown_function — 註冊一個會在php中止時執行的函數 register_shutdown_function( [ $this,‘last_error‘ ]); }
// 異常接管 public function ex($ex) { // 獲取錯誤異常信息 $message = $ex->getMessage(); // 獲取錯誤異常代碼 $code = $ex->getCode(); // 獲取錯誤異常文件 $file = $ex->getFile(); // 獲取錯誤異常文件行數 $line = $ex->getLine(); } // 錯誤接管 public function
err( $code, $message,$file ,$line ) { // 記錄日誌 $this->errlog( $code, $message,$file ,$line ); } // 腳本結束前獲取最後錯誤 public function last_error() { // error_get_last — 獲取最後發生的錯誤 $last = error_get_last(); $this->errlog( $last[‘type‘],$last[‘message‘],$last[‘file‘],$last[‘line‘] ); } // 錯誤信息收集並記錄 (參數傳輸的順序不一樣,參數還不一樣) public function errlog( $code, $message,$file ,$line ) { // 拼接錯誤信息 $errstr = date(‘Y-m-d h:i:s‘)."\r\n"; $errstr .= ‘ 錯誤級別:‘.$code."\r\n"; $errstr .= ‘ 錯誤信息:‘.$message."\r\n"; $errstr .= ‘ 錯誤文件:‘.$file."\r\n"; $errstr .= ‘ 錯誤行數:‘.$line."\r\n"; $errstr .= "\r\n"; // error_log — 發送錯誤信息到某個地方 error_log($errstr,3,__DIR__.‘/error.log‘); } }

[php] 錯誤接管類