Exception.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. $exception=$this;
  18. list(
  19. $className,
  20. $functionName,
  21. $tracesAll
  22. ) =(function()use($exception){
  23. $traces=method_exists($exception,'getTraceAsString')
  24. ?($exception->getTraceAsString()??'')
  25. :'';
  26. if(!$traces)return '';
  27. preg_match('/\#0.*?\: (.*?)(-\>|\:\:)(.*?)\(/', $traces, $result);
  28. return [$result[1]??'',$result[3]??'',$traces];
  29. })();
  30. LogService::log(
  31. $className,
  32. $functionName,
  33. ($exception->getMessage()??'')
  34. .'调用堆栈:'.$tracesAll,
  35. Auth::id()??'',
  36. $this->type
  37. );
  38. }
  39. }