exception2log.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. defined('ONLY_ONLY_ONLY') or exit('Access Denied');
  3. define('EEGLOBAL_LOG_PATH',WEB_PHY_ROOT."/logfiles/");
  4. define('EEGLOBAL_LOGLEVEL_ERROR',"error");
  5. define('EEGLOBAL_LOGLEVEL_DEBUG',"debug");
  6. define('EEGLOBAL_LOGLEVEL_INFO',"info");
  7. define('EEGLOBAL_DEFAULT_USERERRORMSG','非常抱歉,系统发生异常,请您重试!');
  8. define('EEGLOBAL_DEFAULT_CATALOGNAME','system');
  9. ini_set("display_startup_errors","Off");//禁止显示错误
  10. ini_set("display_errors","Off");//禁止显示启动错误
  11. ini_set("log_errors","On");//记录错误
  12. error_reporting(-1);
  13. register_shutdown_function('eeglobal_shutdownhandler');
  14. // set_error_handler('eeglobal_deferrorhandler');
  15. // set_exception_handler('eeglobal_defexceptionhandler');
  16. class GeneralException extends \Exception
  17. {
  18. public $errorCode ;
  19. public $friendmsg ;
  20. public $catalog ;
  21. public function __construct($errorCode,$friendmsg,$catalog = EEGLOBAL_DEFAULT_CATALOGNAME,
  22. $code=0,$previous = null) {
  23. $this->errorCode=$errorCode;
  24. $this->friendmsg=$friendmsg;
  25. $this->catalog=$catalog;
  26. if (is_array($friendmsg)) $friendmsg = @json_encode($friendmsg);
  27. parent::__construct($friendmsg, $code, $previous);
  28. }
  29. }
  30. function eeglobal_deferrorhandler($level,$message,$file='',$line=0){
  31. if(error_reporting() & $level){
  32. $exp = new GeneralException('DEFAULT_ERROR_HANDLER',EEGLOBAL_DEFAULT_USERERRORMSG.$message,
  33. EEGLOBAL_DEFAULT_CATALOGNAME,0,null,$file,$line);
  34. eeglobal_exception_handler($exp,false);
  35. }
  36. return true;/* Don't execute PHP internal error handler */
  37. }
  38. function eeglobal_shutdownhandler(){
  39. $err=error_get_last();
  40. if ($err) {
  41. $exp = new GeneralException('DEFAULT_SHUTDOWN_HANDLER',EEGLOBAL_DEFAULT_USERERRORMSG.$err['message'],
  42. EEGLOBAL_DEFAULT_CATALOGNAME,0,null,$err['file'],$err['line']);
  43. eeglobal_exception_handler($exp,false);
  44. }
  45. }
  46. function eeglobal_defexceptionhandler($ex){
  47. eeglobal_exception_handler($ex,false);
  48. }
  49. function eeglobal_exception_handler($ex,$ajaxOutput){
  50. try{
  51. if(!is_object($ex)) return;
  52. $exstackinfo = array();
  53. $errorCode = "General";
  54. $friendmsg = EEGLOBAL_DEFAULT_USERERRORMSG;
  55. $catalog = EEGLOBAL_DEFAULT_CATALOGNAME;
  56. do {
  57. $clsname=get_class($ex);
  58. $errorCode = ($clsname=='GeneralException'?$ex->errorCode : $errorCode);
  59. $friendmsg = ($clsname=='GeneralException'?$ex->friendmsg : $friendmsg);
  60. $catalog = ($clsname=='GeneralException'?$ex->catalog : $catalog);
  61. $logcontent=$ex->getMessage()."\r\n";
  62. $expfile=$ex->getFile();
  63. $expline=$ex->getLine();
  64. $expcode=$ex->getCode();
  65. $logcontent.=" 异常发生在文件:[{$expfile}]的[{$expline}]行 code[{$expcode}] !\r\n";
  66. $exptrace=str_replace("\n", "\r\n ", $ex->getTraceAsString());
  67. $logcontent.=" 异常堆栈上下文:\r\n";
  68. $logcontent.=" {$exptrace}";
  69. array_push($exstackinfo,$logcontent);
  70. } while($ex = $ex->getPrevious());
  71. $strExAll=join("\r\n",$exstackinfo);
  72. eeglobal_log_handler_core($catalog,"error",$strExAll,true);
  73. if($ajaxOutput){
  74. $ajaxRes = new AjaxResult;
  75. $ajaxRes->ErrCode=$errorCode;
  76. $ajaxRes->ErrMsg=$friendmsg;
  77. ob_clean();
  78. ob_start();
  79. header('Content-Type:application/json;charset=UTF-8');
  80. echo @json_encode($ajaxRes);
  81. exit;
  82. }
  83. }catch(Throwable $ex){
  84. }
  85. }
  86. function eeglobal_log_handler_core($catalog,$loglevel,$logcontent,$hasContext=false){
  87. if($hasContext){
  88. $_GP = array();
  89. $_GP = array_merge($_REQUEST, $_COOKIE, $_SERVER, $_GP);
  90. $context = array();
  91. foreach ($_GP as $key => $value) {
  92. if(is_array($value)){
  93. $value=implode($value);
  94. }
  95. array_push($context,"\t[$key]=$value");
  96. }
  97. $strContext="\r\n[上下文相关信息]:\r\n".join("\r\n",$context);
  98. $logcontent .= $strContext;
  99. }
  100. $logPath=EEGLOBAL_LOG_PATH . date("YmdH")."_".$catalog.".log";
  101. error_log(date("H:i:s"). " [{$loglevel}]: $logcontent \r\n\r\n", 3, $logPath);
  102. }
  103. function eeglobal_log_handler($catalog,$loglevel,$logcontent,$hasContext=false){
  104. eeglobal_log_handler_core($catalog,$loglevel,$logcontent,$hasContext);
  105. }