/** * 发送监控数据 * * @param $tag * @param $group * @param string $msg * @return bool */ public function send($tag, $group, $msg = '', $diffTime = '') { $configObj = new Config("config"); $config = $configObj->get("monitor"); if (!$config) { return false; } $obj = new Client($config['server']['host'], $config['server']['port']); if (!$diffTime) { $diffTime = RunTime::runtime(); } $str = []; $str['name'] = $config['name']; $str['group'] = $group; $str['tag'] = $tag; $str['diff_time'] = $diffTime; $str['time'] = time(); $str['msg'] = $msg; $obj->send(json_encode($str)); }
/** * 异步写日志 * @param $data * @param string $filename * @return bool * @throws Exception */ static function info($data, $filename = "log.log") { $configObj = config("config"); $path = $configObj->get("filelogpath"); if (!$path) { throw new Exception("filelogpath not config"); } $path = $path . "/" . KERISY_ENV . "/" . date('Y-m-d') . "/"; if (!is_dir($path)) { mkdir($path, 0777, true); } $filePath = $path . $filename; static $pid; $data = is_string($data) ? $data : json_encode($data); !$pid && ($pid = function_exists('posix_getpid') ? posix_getpid() : mt_rand(1, 9999)); $ip = self::get_server_ip(); $time = \Kerisy\Tool\RunTime::runtime(); $newData = "[" . $ip . "]" . "[" . $pid . "]" . "[" . date('Y-m-d H:i:s') . "][cost:" . $time . "]" . $data; swoole_async_write($filePath, $newData . "\r\n"); return true; }