123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace App\Exceptions;
- use App\Http\Utils\LoggerFactoryUtil;
- use App\Models\RequestLogModel;
- use Exception;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Illuminate\Support\Facades\DB;
- use Tool\ShanTaoTool\MqTool;
- class Handler extends ExceptionHandler
- {
- /**
- * A list of the exception types that are not reported.
- *
- * @var array
- */
- protected $dontReport = [
- //
- ];
- /**
- * A list of the inputs that are never flashed for validation exceptions.
- *
- * @var array
- */
- protected $dontFlash = [
- 'password',
- 'password_confirmation',
- ];
- /**
- * Report or log an exception.
- *
- * @param \Exception $exception
- * @return void
- */
- public function report(Exception $exception)
- {
- parent::report($exception);
- }
- /**
- * Render an exception into an HTTP response.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Exception $exception
- * @return \Illuminate\Http\Response
- */
- public function render($request, Exception $exception)
- {
- //拦截到异常,先做事务回滚
- DB::rollBack();
-
- //拦截异常记录请求日志
- $endTime = microtime(true);
- $startTime=LoggerFactoryUtil::getStartTime();
- if(!$startTime){
- $startTime = $endTime;
- }
- $responseData = json_encode([$exception->getMessage()]);
- //获取自定义的日志
- $requestLog = "";
- if(LoggerFactoryUtil::getMessage()){
- $requestLog = json_encode(LoggerFactoryUtil::getMessage());
- }
- //获取监听到的sql语句
- $requestSqlLog = "";
- if(LoggerFactoryUtil::getSqlMessage()){
- $requestSqlLog = json_encode(LoggerFactoryUtil::getSqlMessage());
- }
- if(env("REQUEST_LOG_FLAG")){
- // RequestLogModel::insert($logData);
- $requestId = LoggerFactoryUtil::getRequestId()?LoggerFactoryUtil::getRequestId():floor(microtime(true)*1000);
- //获取请求数据
- $param = $request->all();
- //获取用户token
- $token = $request->header("X-TOKEN")?$request->header("X-TOKEN"):"";
- $param = array_merge($param,["token"=>$token]);
- //判断请求参数中是否存在traceId
- if(isset($_GET["traceId"])){
- $traceId= $_GET["traceId"];
- }else{
- $traceId = md5(uniqid().time());
- }
- $mqMessage = [
- "requestPath"=>$request->path(),
- "requestParam"=>json_encode($param),
- "requestResponse"=>$responseData,
- "requestSqlLog"=>$requestSqlLog,
- "requestLog"=>$requestLog,
- "requestProjectName"=>"daogou",
- "createdAt"=>$startTime,
- "updatedAt"=>$endTime,
- "requestId"=>$requestId,
- "uniqueTraceId"=>$traceId
- ];
- try{
- MqTool::pushLogMessage(json_encode($mqMessage),"daogouLogKey");
- LoggerFactoryUtil::setIsPush(1);
- }catch (\Exception $exception){}finally{}
- }
- //异常处理
- if($exception instanceof ValidateException){
- return $this->error($exception->getMessage());
- }
- return parent::render($request, $exception);
- }
- /**
- * 错误返回
- * @param $msg
- * @param string $code
- * @param string $url
- */
- public function error($msg,$code="500",$url="")
- {
- return response()->json(
- [
- "status"=>false,
- "data"=>"",
- "code"=>$code,
- "msg"=>$msg,
- "url"=>$url
- ]
- );
- }
- }
|