def add_log(self, item: str): # item为json.dumps(obj) log_item = LogItem(int(time.time()), [('content', item)]) log_req = PutLogsRequest(self.project, self.logstore, topic=self.topic, source=self.source, logitems=[log_item]) self.client.put_logs(log_req)
def test_add_id(self): log_item = LogItem() doc = { "_id": 1, "_source": {}, "_index": "all_data_types", "_type": "_doc" } DocLogItemConverter._add_id(doc, log_item) self.assertSetEqual({("_id", "1")}, set(log_item.contents))
def __report(self, report_data): """ Args: report_data: dict[string]string. """ if not self.__report_logstore: return log = LogItem() for key, data in report_data.items(): log.push_back(key, '%s' % data) req = PutLogsRequest(project=self.__project_name, logstore=self.__report_logstore, logitems=[log]) self.__client.put_logs(req)
def send_log_to_aliyun(logstore, message): """ 向阿里云日志系统 发送log函数 :param message: 将要发送的log字符串 :return: None """ # 构建一个 client 使用 client 实例的方法来操作日志服务 client = LogClient(settings.END_POINT, settings.ACCESS_KEY_ID, settings.ACCESS_KEY) log_item = LogItem() log_item.set_time(int(time.time())) log_item.set_contents([('message', message)]) put_logs_request = PutLogsRequest(settings.PROJECT, logstore, '', '', [log_item]) client.put_logs(put_logs_request) # 发送log
def test_add_type_without_index_field(self): log_item = LogItem() doc = {"_id": 1, "_source": {}, "_index": "all_data_types"} DocLogItemConverter._add_type(doc, log_item) self.assertSetEqual(set(), set(log_item.contents))
def test_add_type(self): log_item = LogItem() doc = {"_type": "_doc"} DocLogItemConverter._add_type(doc, log_item) self.assertSetEqual({("__tag__:__es_type__", "_doc")}, set(log_item.contents))
def test_add_index(self): log_item = LogItem() doc = {"_index": "index1"} DocLogItemConverter._add_index(doc, log_item) self.assertSetEqual({("__tag__:__es_index__", "index1")}, set(log_item.contents))