123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- defined('ONLY_ONLY_ONLY') or exit('Access Denied');
- define('EEGLOBAL_LOG_PATH',WEB_PHY_ROOT."/logfiles/");
- define('EEGLOBAL_LOGLEVEL_ERROR',"error");
- define('EEGLOBAL_LOGLEVEL_DEBUG',"debug");
- define('EEGLOBAL_LOGLEVEL_INFO',"info");
- define('EEGLOBAL_DEFAULT_USERERRORMSG','非常抱歉,系统发生异常,请您重试!');
- define('EEGLOBAL_DEFAULT_CATALOGNAME','system');
- ini_set("display_startup_errors","Off");//禁止显示错误
- ini_set("display_errors","Off");//禁止显示启动错误
- ini_set("log_errors","On");//记录错误
- error_reporting(-1);
- register_shutdown_function('eeglobal_shutdownhandler');
- // set_error_handler('eeglobal_deferrorhandler');
- // set_exception_handler('eeglobal_defexceptionhandler');
- class GeneralException extends \Exception
- {
- public $errorCode ;
- public $friendmsg ;
- public $catalog ;
- public function __construct($errorCode,$friendmsg,$catalog = EEGLOBAL_DEFAULT_CATALOGNAME,
- $code=0,$previous = null) {
- $this->errorCode=$errorCode;
- $this->friendmsg=$friendmsg;
- $this->catalog=$catalog;
- if (is_array($friendmsg)) $friendmsg = @json_encode($friendmsg);
- parent::__construct($friendmsg, $code, $previous);
- }
- }
- function eeglobal_deferrorhandler($level,$message,$file='',$line=0){
- if(error_reporting() & $level){
- $exp = new GeneralException('DEFAULT_ERROR_HANDLER',EEGLOBAL_DEFAULT_USERERRORMSG.$message,
- EEGLOBAL_DEFAULT_CATALOGNAME,0,null,$file,$line);
- eeglobal_exception_handler($exp,false);
- }
- return true;/* Don't execute PHP internal error handler */
- }
- function eeglobal_shutdownhandler(){
- $err=error_get_last();
- if ($err) {
- $exp = new GeneralException('DEFAULT_SHUTDOWN_HANDLER',EEGLOBAL_DEFAULT_USERERRORMSG.$err['message'],
- EEGLOBAL_DEFAULT_CATALOGNAME,0,null,$err['file'],$err['line']);
- eeglobal_exception_handler($exp,false);
- }
- }
- function eeglobal_defexceptionhandler($ex){
- eeglobal_exception_handler($ex,false);
- }
- function eeglobal_exception_handler($ex,$ajaxOutput){
- try{
- if(!is_object($ex)) return;
- $exstackinfo = array();
- $errorCode = "General";
- $friendmsg = EEGLOBAL_DEFAULT_USERERRORMSG;
- $catalog = EEGLOBAL_DEFAULT_CATALOGNAME;
- do {
- $clsname=get_class($ex);
- $errorCode = ($clsname=='GeneralException'?$ex->errorCode : $errorCode);
- $friendmsg = ($clsname=='GeneralException'?$ex->friendmsg : $friendmsg);
- $catalog = ($clsname=='GeneralException'?$ex->catalog : $catalog);
- $logcontent=$ex->getMessage()."\r\n";
- $expfile=$ex->getFile();
- $expline=$ex->getLine();
- $expcode=$ex->getCode();
- $logcontent.=" 异常发生在文件:[{$expfile}]的[{$expline}]行 code[{$expcode}] !\r\n";
- $exptrace=str_replace("\n", "\r\n ", $ex->getTraceAsString());
- $logcontent.=" 异常堆栈上下文:\r\n";
- $logcontent.=" {$exptrace}";
- array_push($exstackinfo,$logcontent);
- } while($ex = $ex->getPrevious());
- $strExAll=join("\r\n",$exstackinfo);
- eeglobal_log_handler_core($catalog,"error",$strExAll,true);
- if($ajaxOutput){
- $ajaxRes = new AjaxResult;
- $ajaxRes->ErrCode=$errorCode;
- $ajaxRes->ErrMsg=$friendmsg;
- ob_clean();
- ob_start();
- header('Content-Type:application/json;charset=UTF-8');
- echo @json_encode($ajaxRes);
- exit;
- }
- }catch(Throwable $ex){
- }
- }
- function eeglobal_log_handler_core($catalog,$loglevel,$logcontent,$hasContext=false){
- if($hasContext){
- $_GP = array();
- $_GP = array_merge($_REQUEST, $_COOKIE, $_SERVER, $_GP);
- $context = array();
- foreach ($_GP as $key => $value) {
- if(is_array($value)){
- $value=implode($value);
- }
- array_push($context,"\t[$key]=$value");
- }
- $strContext="\r\n[上下文相关信息]:\r\n".join("\r\n",$context);
- $logcontent .= $strContext;
- }
- $logPath=EEGLOBAL_LOG_PATH . date("YmdH")."_".$catalog.".log";
- error_log(date("H:i:s"). " [{$loglevel}]: $logcontent \r\n\r\n", 3, $logPath);
- }
- function eeglobal_log_handler($catalog,$loglevel,$logcontent,$hasContext=false){
- eeglobal_log_handler_core($catalog,$loglevel,$logcontent,$hasContext);
- }
|