laravel实现类方法邮件发送功能,监听框架异常错误

/**
     * 发送邮件错误日志
     * @param $method 方法
     * @param $params 所调用方法的参数
     * @param $result crm接口返回结果
     */
    public static function sendErrorMail($method, $params, $result)
    {
        $method = $method;
        $params = var_export($params, true);
        $result = var_export($result, true);
        $errTotal = Cache::get('crmTotal', 1);

        $message = "接口调用失败.
                失败次数:$errTotal
                接口名:$method
                请求参数:$params
                返回结果:$result
        ";

        $toUsers = [

        ];

        if(Cache::get('sendMailTime',0) + 60 < time()){
            Cache::forever('sendMailTime', time());
            Cache::forever('crmTotal', 1);

            Mail::raw($message,function ($mail)use($toUsers){

                $mail->to('aicken.peng@etocrm.com')->subject('CC接口错误报警');
                foreach($toUsers as $user){
                    $mail->cc($user);
                }
            });

        }else{
            Cache::increment('crmTotal');
        }


    }

//记录错误日志方法
public static function writelog($method, $data, $result , $use_time)
    {
        $file_name = storage_path('/logs/crm/'.date('Y-m-d').'.log');
        if(!file_exists($file_name)){
            mkdir($file_name, 0777, true);
        }

        $strLog = '';
        foreach(array_merge([date('Y-m-d H:i:s')], func_get_args()) as $str) {
            if(is_array($str)) {
                $strLog .= "( " . implode("\t", $str) . " ) \t";
            } else {
                $strLog .= $str . "\t";
            }
        }

        file_put_contents($file_name, $strLog . "\n", FILE_APPEND);
    }

laravel框架的异常处理主要靠app/Exceptions/Handler.php处理。重写这个类的两个方法即可

    public function report(Exception $exception)
    {
        if(empty(env('APP_DEBUG')) && $exception->getMessage()){//判断是否开启调试模式,以及是否有错误信息
            \Mail::raw($exception . 'server:' . json_encode(\Request::server()), function ($m) {
                $m->subject('CC错误监控');
                $m->to('aicken.peng@etocrm.com');
            });
        }
        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)
    {
        if(env('APP_DEBUG')){//判断是否开启调试模式
            return parent::render($request, $exception);
        }

        $msg = empty($exception->getMessage()) ? $exception->getMessage() : '好像出错啦!';
        if($request->ajax()){//判断是否为ajax请求
            return response()->json(\Tool::outErrcode('99', $msg));
        }else{//直接抛出错误页面
            return parent::render($request, $exception);
        }
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值