您现在的位置是:首页 > 博客日记 > Php Php

Laravel 捕捉异常日志发送邮件通知

2019-09-16 18:44:47 【Php】 人已围观

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

163 邮箱的配置

修改.env配置文件

  1. MAIL_DRIVER=smtp
  2. MAIL_HOST=smtp.163.com
  3. MAIL_PORT=25
  4. MAIL_USERNAME=xxx@163.com
  5. MAIL_PASSWORD=xxx
  6. MAIL_ENCRYPTION=null
  7. MAIL_FROM_ADDRESS=xxx@163.com
  8. MAIL_FROM_NAME=xxx

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

  1. /**
  2. * Report or log an exception.
  3. *
  4. * This is a great spot to send exceptions to Emails.
  5. *
  6. * @param \Exception $exception
  7. * @return void
  8. */
  9. public function report(Exception $exception)
  10. {
  11. if ($this->shouldReport($exception)) {
  12. $this->sendEmail($exception); // sends an email
  13. }
  14. return parent::report($exception);
  15. }
  16. /**
  17. * Sends an email to the developer about the exception.
  18. *
  19. * @param \Exception $exception
  20. * @return void
  21. */
  22. public function sendEmail(Exception $exception)
  23. {
  24. // sending email
  25. }

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

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

  1. $ php artisan make:mail ExceptionOccured

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

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

修改 App\Exceptions\Handler

  1. public function sendEmail(Exception $exception)
  2. {
  3. try {
  4. $e = FlattenException::create($exception);
  5. $handler = new SymfonyExceptionHandler();
  6. $html = $handler->getHtml($e);
  7. Mail::to('developer@gmail.com')->send(new ExceptionOccured($html));
  8. } catch (Exception $ex) {
  9. dd($ex);
  10. }
  11. }

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

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

  1. use Mail;
  2. use Symfony\Component\Debug\Exception\FlattenException;
  3. use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
  4. use App\Mail\ExceptionOccured;

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

  1. <?php
  2. namespace App\Mail;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Mail\Mailable;
  5. use Illuminate\Queue\SerializesModels;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. class ExceptionOccured extends Mailable
  8. {
  9. use Queueable, SerializesModels;
  10. /**
  11. * The body of the message.
  12. *
  13. * @var string
  14. */
  15. public $content;
  16. /**
  17. * Create a new message instance.
  18. *
  19. * @return void
  20. */
  21. public function __construct($content)
  22. {
  23. $this->content = $content;
  24. }
  25. /**
  26. * Build the message.
  27. *
  28. * @return $this
  29. */
  30. public function build()
  31. {
  32. return $this->view('emails.exception')
  33. ->with('content', $this->content);
  34. }
  35. }

在emails.exception视图文件中添加以下代码:

  1. {!! $content !!}

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

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

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

  1. 530 5.7.1 Authentication required

清除配置缓存

  1. php artisan config:clear

并重启 php artisan serve



关注TinyMeng博客,更多精彩分享,敬请期待!
 

很赞哦! ()