123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace App\Exceptions;
- use App\Log;
- use App\Support\EmailQueue;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Throwable;
- class Handler extends ExceptionHandler
- {
- /**
- * A list of the exception types that are not reported.
- *
- * @var array<int, class-string<Throwable>>
- */
- protected $dontReport = [
- //
- ];
- private $_isNoticed = false;
- /**
- * A list of the inputs that are never flashed for validation exceptions.
- *
- * @var array<int, string>
- */
- protected $dontFlash = [
- 'current_password',
- 'password',
- 'password_confirmation',
- ];
- /**
- * Register the exception handling callbacks for the application.
- *
- * @return void
- */
- public function register()
- {
- $this->reportable(function (Throwable $e) {
- });
- }
- /**
- * 发送异常通知
- * @param Throwable $exception
- */
- public function notice(Throwable $exception)
- {
- try {
- $subject = config('app.name') . '于' . date('d日H:i:s') . '抛出异常';
- $content = $exception->getMessage() . "\r\n\r\n" . $exception->getTraceAsString();
- Log::error($subject, ['message' => $content], 'exception');
- if (config('app.env') === 'production') {
- $email = env('EXCEPTION_REPORT_EMAIL');
- if (empty($email)) {
- return;
- }
- $emailList = array_values(array_unique(array_filter(array_map('trim', explode(',', $email)))));
- EmailQueue::rPush($subject, nl2br($content), $emailList, config('app.name'));
- }
- } catch (\Throwable $e) {
- Log::error('发送异常通知失败!', [
- 'message' => $e->getMessage() . "\r\n" . $e->getTraceAsString()
- ], 'exception');
- // print_r($e->getMessage());
- }
- }
- // 上报异常至错误driver,如日志文件(storage/logs/laravel.log),第三方日志存储分析平台
- public function report(Throwable $exception)
- {
- if ($this->shouldReport($exception) && ! $this->_isNoticed) {
- // 发送异常通知
- $this->notice($exception); // sends an email
- $this->_isNoticed = true;
- }
- 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, Throwable $exception)
- {
- return parent::render($request, $exception);
- }
- }
|