laravel 异常捕获_Laravel 捕捉异常日志发送邮件通知

由于程序出错我们并不能第一时间发现错误并修改错误,特别是服务比较多的情况下,不可能每个服务都去线上查看日志,节假日可以及时接收服务异常信息,所以我们配置了laravel框架捕捉错误并将异常以邮箱方式通知程序开发人员,来及时修改这些bug。

163 邮箱的配置

修改.env配置文件 MAIL_DRIVER=smtp

MAIL_HOST=smtp.163.com

MAIL_PORT=25

MAIL_USERNAME=xxx@163.com

MAIL_PASSWORD=xxx

MAIL_ENCRYPTION=null

MAIL_FROM_ADDRESS=xxx@163.com

MAIL_FROM_NAME=xxx

在Laravel中,所有异常都由App\Exceptions\Handler类处理。这个类包含两个方法:report和render。我们只对report方法; 它用于捕捉异常或将它们发送到Bugsnag或Sentry等外部服务。默认情况下,report方法只是将异常传递给记录异常的基类。但是,我们可以使用它向开发人员发送有关异常的电子邮件。 /**

* Report or log an exception.

*

* This is a great spot to send exceptions to Emails.

*

* @param \Exception $exception

* @return void

*/

publicfunctionreport(Exception$exception)

{

if($this->shouldReport($exception)){

$this->sendEmail($exception);// sends an email

}

returnparent::report($exception);

}

/**

* Sends an email to the developer about the exception.

*

* @param \Exception $exception

* @return void

*/

publicfunctionsendEmail(Exception$exception)

{

// sending email

}

这里我们使用shouldReport方法来忽略$dontReport异常处理程序属性中列出的异常。

应用程序发送的每种类型的电子邮件都表示为Laravel中的“email”类。因此,我们需要使用以下make:mail命令创建可email的类: $ php artisan make:mailExceptionOccured

将在app/Mail目录中创建一个ExceptionOccured类。

仅发送邮件无法解决问题。我们需要异常的完整堆栈跟踪。为此,我们可以使用Symfony的Debug组件

修改 App\Exceptions\Handler类 publicfunctionsendEmail(Exception$exception)

{

try{

$e=FlattenException::create($exception);

$handler=newSymfonyExceptionHandler();

$html=$handler->getHtml($e);

Mail::to('developer@gmail.com')->send(newExceptionOccured($html));

}catch(Exception$ex){

dd($ex);

}

}注意 - try如果mail发送失败,我们使用了catch避免错误无限循环

确保在文件顶部添加引用类: useMail;

useSymfony\Component\Debug\Exception\FlattenException;

useSymfony\Component\Debug\ExceptionHandlerasSymfonyExceptionHandler;

useApp\Mail\ExceptionOccured;

然后,在ExceptionOccured邮件类中修改: <?php

namespaceApp\Mail;

useIlluminate\Bus\Queueable;

useIlluminate\Mail\Mailable;

useIlluminate\Queue\SerializesModels;

useIlluminate\Contracts\Queue\ShouldQueue;

classExceptionOccuredextendsMailable

{

useQueueable,SerializesModels;

/**

* The body of the message.

*

* @var string

*/

public$content;

/**

* Create a new message instance.

*

* @return void

*/

publicfunction__construct($content)

{

$this->content=$content;

}

/**

* Build the message.

*

* @return $this

*/

publicfunctionbuild()

{

return$this->view('emails.exception')

->with('content',$this->content);

}

}

在emails.exception视图文件中添加以下代码: {!!$content!!}

在主程序中写一个不存在的变量echo $aaa;

现在,每当您的应用程序中抛出异常时,您将收到一封包含完整堆栈跟踪的电子邮件。

注: 模拟一个异常,会发现异常邮件发送失败,报错信息是 5305.7.1Authenticationrequired

清除配置缓存 php artisan config:clear

并重启 php artisan serve

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值