Exception.php 1.1 KB

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