12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace app\common\utility;
- use think\Log;
- define('CPS_FATAL', E_ERROR | E_USER_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_RECOVERABLE_ERROR| E_PARSE | E_STRICT);
- class CPSError{
- private $errCode = array (
- E_ERROR => 'Error',
- E_WARNING => 'Warning',
- E_PARSE => 'Parsing Error',
- E_NOTICE => 'Notice',
- E_CORE_ERROR => 'Core Error',
- E_CORE_WARNING => 'Core Warning',
- E_COMPILE_ERROR => 'Compile Error',
- E_COMPILE_WARNING => 'Compile Warning',
- E_USER_ERROR => 'User Error',
- E_USER_WARNING => 'User Warning',
- E_USER_NOTICE => 'User Notice',
- E_STRICT => 'Runtime Notice',
- E_RECOVERABLE_ERROR => 'Catchable Fatal Error'
- );
- /**
- * 单例对象
- */
- protected static $instance;
- /**
- * 初始化
- * @access public
- */
- public static function hanndle(){
- if (is_null(self::$instance)) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 构造函数
- * CPSError constructor.
- */
- private function __construct(){
- error_reporting(E_ALL);
- @register_shutdown_function([$this,'hanndleFatal']);
- @set_error_handler([$this, 'writeLogAndThrowExcption']);
- }
- /**
- * 错误处理
- * @throws \Exception
- */
- public function hanndleFatal(){
- $error = error_get_last();
- if($error && ($error["type"]===($error["type"] & CPS_FATAL))) {
- $this->writeLogAndThrowExcption($error["type"], $error["message"], $error["file"], $error["line"]);
- }
- }
- /**
- * 写日志信息
- * @param $errno
- * @param $errmsg
- * @param $filename
- * @param $linenum
- * @throws \Exception
- */
- public function writeLogAndThrowExcption($errno, $errmsg, $filename, $linenum)
- {
- Log::error('CPSException: Type:'.($this->errCode[$errno] ?? '未知错误').' Line:'.$linenum.' File:'.$filename.' Msg:'.$errmsg);
- throw new \Exception($errmsg);
- }
- public function __destruct(){
- restore_error_handler();
- }
- }
|