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, Exception $exception) { return parent::render($request, $exception); } /** * Convert an authentication exception into an unauthenticated response. * * @param \Illuminate\Http\Request $request * @param \Illuminate\Auth\AuthenticationException $exception * @return \Illuminate\Http\Response */ protected function unauthenticated($request, AuthenticationException $exception) { if ($request->expectsJson()) { return response()->json(['error' => 'Unauthenticated.'], 401); } return redirect()->guest(route('login')); } /** * 发送异常通知 * @param Exception $exception */ public function notice(Exception $exception) { try { $subject = config('app.name') . '于' . date('d日H:i:s') . '抛出异常'; $content = $exception->getMessage() . "\r\n\r\n" . $exception->getTraceAsString(); Log::logError($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::logError('发送异常通知失败!', [ 'message' => $e->getMessage() . "\r\n" . $e->getTraceAsString() ], 'exception'); // print_r($e->getMessage()); } } }