Exception.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Services\LogService;
  4. use Illuminate\Support\Facades\Auth;
  5. use Throwable;
  6. class Exception extends \Exception
  7. {
  8. public $type;
  9. public function __construct($message = "",$type='error', $code = 0, Throwable $previous = null)
  10. {
  11. parent::__construct($message, $code, $previous);
  12. $this->type=$type;
  13. $this->logging();
  14. }
  15. public function logging()
  16. {
  17. \Illuminate\Support\Facades\Log::info("抓取内存耗尽错误",debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT,5));
  18. if (substr($this->getFile(),-(strlen("FatalError.php")))==='FatalError.php'){
  19. \Illuminate\Support\Facades\Log::emergency("FPM内存耗尽",["user"=>Auth::id(),"param"=>request()]);
  20. }
  21. $exception=$this;
  22. list(
  23. $className,
  24. $functionName,
  25. $tracesAll
  26. )
  27. =(function()use($exception){
  28. $traces=method_exists($exception,'getTraceAsString')
  29. ?($exception->getTraceAsString()??'')
  30. :'';
  31. if(!$traces)return '';
  32. preg_match('/\#0.*?\: (.*?)(-\>|\:\:)(.*?)\(/', $traces, $result);
  33. return [$result[1]??'',$result[3]??'',$traces];
  34. })();
  35. LogService::log(
  36. $className,
  37. $functionName,
  38. ($exception->getMessage()??'')
  39. .'调用堆栈e:'.$tracesAll,
  40. Auth::id()??'',
  41. $this->type
  42. );
  43. }
  44. }