Handler.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Http\Utils\LoggerFactoryUtil;
  4. use App\Models\RequestLogModel;
  5. use Exception;
  6. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  7. use Illuminate\Support\Facades\DB;
  8. use Tool\ShanTaoTool\MqTool;
  9. class Handler extends ExceptionHandler
  10. {
  11. /**
  12. * A list of the exception types that are not reported.
  13. *
  14. * @var array
  15. */
  16. protected $dontReport = [
  17. //
  18. ];
  19. /**
  20. * A list of the inputs that are never flashed for validation exceptions.
  21. *
  22. * @var array
  23. */
  24. protected $dontFlash = [
  25. 'password',
  26. 'password_confirmation',
  27. ];
  28. /**
  29. * Report or log an exception.
  30. *
  31. * @param \Exception $exception
  32. * @return void
  33. */
  34. public function report(Exception $exception)
  35. {
  36. parent::report($exception);
  37. }
  38. /**
  39. * Render an exception into an HTTP response.
  40. *
  41. * @param \Illuminate\Http\Request $request
  42. * @param \Exception $exception
  43. * @return \Illuminate\Http\Response
  44. */
  45. public function render($request, Exception $exception)
  46. {
  47. //拦截到异常,先做事务回滚
  48. DB::rollBack();
  49. //拦截异常记录请求日志
  50. $endTime = microtime(true);
  51. $startTime=LoggerFactoryUtil::getStartTime();
  52. if(!$startTime){
  53. $startTime = $endTime;
  54. }
  55. $responseData = json_encode([$exception->getMessage(),$exception->getTraceAsString()]);
  56. //获取自定义的日志
  57. $requestLog = "";
  58. if(LoggerFactoryUtil::getMessage()){
  59. $requestLog = json_encode(LoggerFactoryUtil::getMessage());
  60. }
  61. //获取监听到的sql语句
  62. $requestSqlLog = "";
  63. if(LoggerFactoryUtil::getSqlMessage()){
  64. $requestSqlLog = json_encode(LoggerFactoryUtil::getSqlMessage());
  65. }
  66. if(env("REQUEST_LOG_FLAG")){
  67. // RequestLogModel::insert($logData);
  68. $requestId = LoggerFactoryUtil::getRequestId()?LoggerFactoryUtil::getRequestId():floor(microtime(true)*1000);
  69. //获取请求数据
  70. $param = $request->all();
  71. //获取用户token
  72. $token = $request->header("X-TOKEN")?$request->header("X-TOKEN"):"";
  73. $param = array_merge($param,["token"=>$token]);
  74. //判断请求参数中是否存在traceId
  75. if(isset($_GET["traceId"])){
  76. $traceId= $_GET["traceId"];
  77. }else{
  78. $traceId = md5(uniqid().time());
  79. }
  80. $mqMessage = [
  81. "requestPath"=>$request->path(),
  82. "requestParam"=>json_encode($param),
  83. "requestResponse"=>$responseData,
  84. "requestSqlLog"=>$requestSqlLog,
  85. "requestLog"=>$requestLog,
  86. "requestProjectName"=>"daogou",
  87. "createdAt"=>$startTime,
  88. "updatedAt"=>$endTime,
  89. "requestId"=>$requestId,
  90. "uniqueTraceId"=>$traceId
  91. ];
  92. try{
  93. MqTool::pushLogMessage(json_encode($mqMessage),"daogouLogKey");
  94. LoggerFactoryUtil::setIsPush(1);
  95. }catch (\Exception $exception){}finally{}
  96. }
  97. //异常处理
  98. if($exception instanceof ValidateException){
  99. return $this->error($exception->getMessage());
  100. }else{
  101. return $this->error($exception->getMessage());
  102. }
  103. // return parent::render($request, $exception);
  104. }
  105. /**
  106. * 错误返回
  107. * @param $msg
  108. * @param string $code
  109. * @param string $url
  110. */
  111. public function error($msg,$code="500",$url="")
  112. {
  113. return response()->json(
  114. [
  115. "status"=>false,
  116. "data"=>"",
  117. "code"=>$code,
  118. "msg"=>$msg,
  119. "url"=>$url
  120. ]
  121. );
  122. }
  123. }