php後臺管理系統laravel7.2以上異常傳送釘釘告警提醒
阿新 • • 發佈:2022-05-18
1,\app\Http\Middleware\目錄下新增檔案Monitor.php
<?php namespace App\Http\Middleware; use Error; use Closure; use Exception; use Illuminate\Http\Request; use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Debug\ExceptionHandler; use Psy\Exception\ErrorException; use Psy\Exception\FatalErrorException;View Codeclass Monitor { /** * The App container * * @var Container */ protected $container; /** * The Monitor Client * * @var */ protected $monitor; /** * Create a new middleware instance. * * @param Container $container */ publicfunction __construct(Container $container) { $this->container = $container; } /** * Handle an incoming request. * * @param Request $request * @param Closure $next * @return mixed */ public function handle($request, Closure $next) { // dump("monitorDing");$enabled = config('monitorDing.enabled'); try { $response = $next($request); } catch (Exception $e) { $response = $this->handleException($request, $e); $enabled && $this->sendText(sprintf("檔案:%s (%s 行) 內容:%s", $e->getFile(), $e->getLine(), $e->getMessage())); } catch (Error $error) { $e = new FatalErrorException($error); $response = $this->handleException($request, $e); $enabled && $this->sendText(sprintf("檔案:%s (%s 行) 內容:%s", $e->getFile(), $e->getLine(), $e->getMessage())); } catch (ErrorException $error) { $e = new FatalErrorException($error); $response = $this->handleException($request, $e); $enabled && $this->sendText(sprintf("檔案:%s (%s 行) 內容:%s", $e->getFile(), $e->getLine(), $e->getMessage())); } finally { if ($response->getStatusCode() == '500' && (isset($response->exception) && $response->exception && $response->exception !== null)) { $sysName = config('monitorDing.web_name'); if (strpos($_SERVER['HTTP_HOST'], 'liexin') !== false) { $sysName = '本地' . $sysName; } else { $sysName = '外網' . $sysName; } $this->sendText(substr($sysName . ":" . $response->exception, 0, 500) . ',請求資料:' . json_encode($request->input()) . "---[更多詳情請看日誌]"); } } return $response; } /** * Handle the given exception. * * (Copy from Illuminate\Routing\Pipeline by Taylor Otwell) * * @param $passable * @param Exception $e * @return mixed * @throws Exception */ protected function handleException($passable, Exception $e) { if (!$this->container->bound(ExceptionHandler::class) || !$passable instanceof Request) { throw $e; } $handler = $this->container->make(ExceptionHandler::class); $handler->report($e); return $handler->render($passable, $e); } /** * 傳送文字型別的訊息 * * @param $content string 訊息內容 * @param array $atMobiles 被@人的手機號 * @param bool $isAtAll 是否 @ 所有人 * @throws SendErrorException */ public function sendText($content, $atMobiles = [], $isAtAll = false) { $params = [ 'msgtype' => 'text', 'text' => [ 'content' => $content, ], 'at' => [ 'atMobiles' => $atMobiles, 'isAtAll' => $isAtAll ] ]; $this->send($params); } /** * 傳送 * @param array $params 請求需要的引數 * @throws SendErrorException */ private function send($params = []) { if (!config('monitorDing.enabled')) { \Log::info('~~ Monitor Ding ~~'); \Log::info($params); } else { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, config("monitorDing.webhook")); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;charset=utf-8')); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); if (config()) { curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); } $data = json_decode(curl_exec($ch), true); curl_close($ch); if ($data['errcode']) { // throw new SendErrorException($data['errmsg']); } } } }
2,Kernel.php新增\App\Http\Middleware\Monitor::class
3,,config檔案新增monitorDing.php
<?php return [ // 是否開啟報錯寫入 'enabled' => true, // curl證書驗證, 線下環境不用開啟 'curl_verify' =>"false", 'web_name'=>"釘釘告警關鍵詞", // webhook的值 'webhook' => "釘釘告警地址", ];
完成