php错误封装类
1、創建MyErrorHandler.php文件
代碼如下:
<?php class MyErrorHandler {public $message;public $filename;public $line;public $vars = array();protected $_noticeLog = 'F:\root\noticeLog.log';public function __construct ($message,$filename,$line,$vars) {$this->message = $message;$this->filename = $filename;$this->line = $line;$this->vars = $vars;}public static function deal ($errno,$errmsg,$filename,$line,$vars) {$self = new self($errno,$errmsg,$filename,$line,$vars);switch ($errno) {case E_USER_ERROR:return $self->dealError();break;case E_USER_WARNING:case E_WARNING:return $self->dealWarning();break;case E_NOTICE:case E_USER_NOTICE:return $self->dealNotice();default:return false;}}/*** 致命的錯誤*/public function dealError() {ob_start();debug_print_backtrace();$backtrace = ob_get_flush();$errorMsg = <<<EOF 出現了致命的錯誤,如下: 產生錯誤的文件:{$this->filename} 產生錯誤的信息:{$this->message} 產生錯誤的行號:{$this->line} 追蹤信息{$backtrace} EOF;error_log($errorMsg,1,'279921301@qq.com','From:php[error錯誤日志]');exit(1);}/*** 警告的錯誤*/public function dealWarning() {$errorMsg = <<<EOF 出現了警告的錯誤,如下: 產生警告的文件:{$this->filename} 產生警告的信息:{$this->message} 產生警告的行號:{$this->line} EOF;return error_log($errorMsg,1,'279921301@qq.com','From:php[error警告日志]');}/*** 通知的錯誤*/public function dealNotice() {$datetime = date('Y-m-d H:i:s');$errorMsg = <<<EOF 出現了通知的錯誤,如下: 產生通知的文件:{$this->filename} 產生通知的信息:{$this->message} 產生通知的行號:{$this->line} 產生通知的時間:{$datetime} EOF;return error_log($errorMsg,3,$this->_noticeLog);} }
2、測試代碼
<?php include "MyErrorHandler.php";//報告所有 PHP 錯誤 error_reporting(-1);//不顯示錯誤信息 ini_set('display_errors',0); set_error_handler(array('MyErrorHandler','deal'));echo $test; //Notice錯誤 settype($var,'test'); //警告錯誤 test();
注意:發送郵件需要在本地配置mail函數,可以參考本人的上一篇文章如何配置mail函數發郵件。
轉載于:https://www.cnblogs.com/gide/p/4549785.html
總結
- 上一篇: 《微之宅残牡丹》是谁的作品?
- 下一篇: 苹果手机换卡槽多少钱