Laravel 5 Send Errors to Email

前端 未结 9 647
野的像风
野的像风 2021-02-03 10:45

I am trying to figure out how I can send errors to my email in Laravel 5. I haven\'t had much luck finding any good resources.

There used to be good packages like: http

9条回答
  •  死守一世寂寞
    2021-02-03 10:56

    I expanded on West55's answer, to include some request info in the email. Useful to know, especially when you want to try and duplicate the error yourself. It's wrapped in a try/catch just to make sure we don't throw another exception while getting request info.

     public function report(Exception $e)
    {
        if ($e instanceof Exception){
    
            $debugSetting = \Config::get('app.debug');
            // email err if debug off (in prod env)
            if (!$debugSetting){
                \Config::set('app.debug', true);
                if (ExceptionHandler::isHttpException($e)) {
                    $content = ExceptionHandler::toIlluminateResponse(ExceptionHandler::renderHttpException($e), $e);
                } else {
                    $content = ExceptionHandler::toIlluminateResponse(ExceptionHandler::convertExceptionToResponse($e), $e);
                }
                \Config::set('app.debug', $debugSetting);
    
                $lc2 = (isset($content->original)) ? $content->original : $e->getMessage();
    
                // add request info to err msg
                $lc1= '';
                try{
                    $request= request();
                    $lc1=
                        "
    " . "-- Request --
    " . "
    Method: " . $request->getMethod() . "
    Uri: " . $request->getUri() . "
    Ip: " . $request->getClientIp() . "
    Referer: " . $request->server('HTTP_REFERER') . "
    Is secure: " . $request->isSecure() . "
    Is ajax: " . $request->ajax() . "
    User agent: " . $request->server('HTTP_USER_AGENT') . "
    Content:
    " . nl2br(htmlentities($request->getContent())) . "
    "; }catch (Exception $e2){} if (strpos($lc2, '') !== false){ $lc2= str_replace('', $lc1 . '', $lc2); }else{ $lc2.= $lc1; } $la2['content']= $lc2; //put into array for blade $ln1= Mail::send('emails.exception2', $la2, function($m){ $m->to('support@mydomain.com') ->subject('Fatal Error'); }); } } parent::report($e); }

提交回复
热议问题