exception2log.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 $userfilename = "";
  22. public $userfileline = "";
  23. public function __construct($errorCode, $friendmsg, $catalog = EEGLOBAL_DEFAULT_CATALOGNAME,
  24. $code = 0, $previous = null, $file = '', $line = 0) {
  25. $this->errorCode = $errorCode;
  26. $this->friendmsg = $friendmsg;
  27. $this->catalog = $catalog;
  28. if (is_array($friendmsg)) {
  29. $friendmsg = @json_encode($friendmsg);
  30. }
  31. parent::__construct($friendmsg, $code, $previous);
  32. $this->userfilename = $file;
  33. $this->userfileline = $line;
  34. }
  35. public function geterrinfo()
  36. {
  37. $msg = $this->userfilename . ":" . $this->userfileline;
  38. return $msg;
  39. }
  40. }
  41. function eeglobal_deferrorhandler($level, $message, $file = '', $line = 0)
  42. {
  43. if (error_reporting() & $level) {
  44. $exp = new GeneralException('DEFAULT_ERROR_HANDLER', EEGLOBAL_DEFAULT_USERERRORMSG . $message,
  45. EEGLOBAL_DEFAULT_CATALOGNAME, 0, null, $file, $line);
  46. eeglobal_exception_handler($exp, false);
  47. }
  48. return true; /* Don't execute PHP internal error handler */
  49. }
  50. function eeglobal_shutdownhandler()
  51. {
  52. $err = error_get_last();
  53. if ($err) {
  54. $exp = new GeneralException('DEFAULT_SHUTDOWN_HANDLER', EEGLOBAL_DEFAULT_USERERRORMSG . $err['message'],
  55. EEGLOBAL_DEFAULT_CATALOGNAME, 0, null, $err['file'], $err['line']);
  56. eeglobal_exception_handler($exp, false);
  57. }
  58. }
  59. function eeglobal_defexceptionhandler($ex)
  60. {
  61. eeglobal_exception_handler($ex, false);
  62. }
  63. function eeglobal_exception_handler($ex, $ajaxOutput)
  64. {
  65. try {
  66. if (!is_object($ex)) {
  67. return;
  68. }
  69. $exstackinfo = array();
  70. $errorCode = "General";
  71. $friendmsg = EEGLOBAL_DEFAULT_USERERRORMSG;
  72. $catalog = EEGLOBAL_DEFAULT_CATALOGNAME;
  73. do {
  74. $clsname = get_class($ex);
  75. $errorCode = ($clsname == 'GeneralException' ? $ex->errorCode : $errorCode);
  76. $friendmsg = ($clsname == 'GeneralException' ? $ex->friendmsg : $friendmsg);
  77. $catalog = ($clsname == 'GeneralException' ? $ex->catalog : $catalog);
  78. $logcontent = $ex->getMessage() . "\r\n";
  79. $expfile = $ex->getFile();
  80. $expline = $ex->getLine();
  81. $expcode = $ex->getCode();
  82. $logcontent .= " 异常发生在文件:[{$expfile}]的[{$expline}]行 code[{$expcode}] !\r\n";
  83. $exptrace = str_replace("\n", "\r\n ", $ex->getTraceAsString());
  84. $logcontent .= " 异常堆栈上下文:\r\n";
  85. $logcontent .= " {$exptrace}";
  86. $loginfo = $ex->geterrinfo();
  87. $logcontent .= "错误信息" . "\r\n" . $loginfo;
  88. array_push($exstackinfo, $logcontent);
  89. } while ($ex = $ex->getPrevious());
  90. $strExAll = join("\r\n", $exstackinfo);
  91. eeglobal_log_handler_core($catalog, "error", $strExAll, true);
  92. if ($ajaxOutput) {
  93. $ajaxRes = new AjaxResult;
  94. $ajaxRes->ErrCode = $errorCode;
  95. $ajaxRes->ErrMsg = $friendmsg;
  96. ob_clean();
  97. ob_start();
  98. header('Content-Type:application/json;charset=UTF-8');
  99. echo @json_encode($ajaxRes);
  100. exit;
  101. }
  102. } catch (Throwable $ex) {
  103. }
  104. }
  105. function eeglobal_log_handler_core($catalog, $loglevel, $logcontent, $hasContext = false)
  106. {
  107. if ($hasContext) {
  108. $_GP = array();
  109. $_GP = array_merge($_REQUEST, $_COOKIE, $_SERVER, $_GP);
  110. $context = array();
  111. foreach ($_GP as $key => $value) {
  112. if (is_array($value)) {
  113. $value = implode($value);
  114. }
  115. array_push($context, "\t[$key]=$value");
  116. }
  117. $strContext = "\r\n[上下文相关信息]:\r\n" . join("\r\n", $context);
  118. $logcontent .= $strContext;
  119. }
  120. $logPath = EEGLOBAL_LOG_PATH . date("YmdH") . "_" . $catalog . ".log";
  121. error_log(date("H:i:s") . " [{$loglevel}]: $logcontent \r\n\r\n", 3, $logPath);
  122. }
  123. function eeglobal_log_handler($catalog, $loglevel, $logcontent, $hasContext = false)
  124. {
  125. eeglobal_log_handler_core($catalog, $loglevel, $logcontent, $hasContext);
  126. }