发送错误消息到bearychat机器人 memory('开始运行'); $this-phpErrorLog = self::PATH . self::ERROR_FILE; $this-LineFile = self::PATH . self::LINE_FILE; $this-color = array( 'fatal' = "#c9302c", 'warning' = "#f0ad4e", 'parse' = "#9
memory('开始运行'); $this->phpErrorLog = self::PATH . self::ERROR_FILE; $this->LineFile = self::PATH . self::LINE_FILE; $this->color = array( 'fatal' => "#c9302c", 'warning' => "#f0ad4e", 'parse' => "#970aff", 'notice' => "#5bc0de", 'strict' => "#545764" ); } public static function getInstance() { if (self::$instance == null) { self::$instance = new self(); } return self::$instance; } public function readLogs() { $this->initFile(); $totalLine = $this->countTotal(); $prevLine = $this->readFile(); $line = intval($totalLine) - intval($prevLine); if ($line <= 0) { return false; } $tailCommand = sprintf(self::TAIL, $line, $this->phpErrorLog); $output = `$tailCommand`; $output = trim($output); if ($output) { $errorType = 'strict'; $logArr = explode("\n", $output); foreach ($logArr as $itemLine) { $errorTime = date("Y-m-d H:i:s"); preg_match("/(\d+)-(\w+)-(\d+) (\d+):(\d+):(\d+)/", $itemLine, $matches); if (isset($matches[0])) { $errorTime = date("Y-m-d H:i:s", strtotime($matches[0])); } preg_match("/PHP (\w+)/", $itemLine, $errorMatches); if (isset($errorMatches[1])) { $errorType = strtolower($errorMatches[1]); } $data = array( "text" => "发送时间: " . $errorTime, "attachments" => array( array( "title" => "错误内容: ", "text" => $itemLine, "color" => isset($this->color[$errorType]) ? $this->color[$errorType] : "strict" ) ) ); $this->curlPost($data); unset($data, $errorType, $errorTime); } } $this->writeFile($totalLine); $this->memory('当前运行'); unset($output, $totalLine, $logArr, $prevLine, $line, $tailCommand); } private function readFile() { $this->initFile(); return @file_get_contents($this->LineFile); } private function writeFile($data) { return @file_put_contents($this->LineFile, $data); } private function initFile() { if ( ! @file_exists($this->phpErrorLog)) { @file_put_contents($this->phpErrorLog, ""); } if ( ! file_exists($this->LineFile)) { $this->writeFile(0); } } private function countTotal() { $totalCommand = sprintf(self::TOTAL_LINE, self::PATH . self::ERROR_FILE); $output = `$totalCommand`; $output = trim($output); return $output; } private function curlPost($text = '') { $commandStr = "curl '%s' -X POST -d 'payload=%s'"; $jsonData = json_encode($text); $command = sprintf($commandStr, self::BEARYCHAT, $jsonData); $output = `$command`; var_dump($output); } public function memory($title = '') { $memory = round(memory_get_usage() / 1024 / 1024, 2); echo $title . ":{$memory} M" . PHP_EOL; } } $logRun = LogRun::getInstance(); while (1) { $logRun->readLogs(); sleep(1); }