php 错误日志 抛出异常

<?php

// ini_set('display_errors', 'on');
// error_reporting(E_ALL|E_STRICT);

error_reporting(0);
// 错误异常类 错误接管
class App{
public function __construct(){
$this->iserr();
}

public function iserr(){
set_exception_handler([$this,'ex']);
set_error_handler([$this,'err']);
//脚本运行结束前调用rsf
register_shutdown_function([$this,'rsf']);

}

public function rsf(){
$er = error_get_last();
if(in_array($er['type'],[1,4,16])){
$this->errorlog($er['type'],$er['message'],$er['file'],$er['line']);
}
}

/**
*异常接管
*/
public function ex($ex){
$msg = $ex->getMessage();
$type = $ex->getCode();
$file = $ex->getFile();
$line = $ex->getline();
// echo '类型:'.$type.'信息:'.$msg.' 位置:'.$file.' 错误行:'.$line;
$this->errorlog($type,$msg,$file,$line);
}

/**
*获取错误信息
*/
public function err($type,$msg,$file,$line){
// echo '类型:'.$type.'信息:'.$msg.' 位置:'.$file.' 错误行:'.$line;
$this->errorlog($type,$msg,$file,$line);
}


/**
*错误日志
*/
public function errorlog($type,$msg,$file,$line){
$errstr = date('Y-m-d H:i:s',time())."\r\n";
$errstr .= '错误级别'.$type."\r\n";
$errstr .= ' 错信息'.$msg."\r\n";
$errstr .= ' 错误文件'.$file."\r\n";
$errstr .= ' 错误行'.$line;
$errstr.='\r\n';
error_log($errstr,3,'err.log');
}
}

new App();
// include ('err.php');
echo $s;
// echo $app->iserr();
// throw new Exception("Error Processing Request", 500);

posted @ 2019-06-15 09:14  yahn~  阅读(367)  评论(0编辑  收藏  举报