全部產品
Search
文件中心

Simple Log Service:C++ SDK快速入門

更新時間:Jun 30, 2024

本文介紹如何快速使用Log ServiceC++ SDK。

前提條件

  • 已建立RAM使用者並完成授權。具體操作,請參見建立RAM使用者並完成授權

  • 已配置環境變數ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。具體操作,請參見在Linux、macOS和Windows系統配置環境變數

    重要
    • 阿里雲帳號的AccessKey擁有所有API的存取權限,建議您使用RAM使用者的AccessKey進行API訪問或日常營運。

    • 強烈建議不要把AccessKey ID和AccessKey Secret儲存到工程代碼裡,否則可能導致AccessKey泄露,威脅您帳號下所有資源的安全。

  • 已安裝C++ SDK安裝。具體操作,請參見安裝C++ SDK

範例程式碼

LOGClient是Log Service的C++用戶端,用於管理Project、Logstore等Log Service資源。使用C++ SDK發起Log Service請求,您需要初始化一個Client執行個體。範例程式碼如下所示:

#include "client.h"
#include "common.h"
#include <string>
#include <cstdlib>
#include <iostream>
#include <unistd.h>
using namespace aliyun_log_sdk_v6;
using namespace std;
int main(int argc,char ** argv)
{
    // Log Service的服務存取點。此處以杭州為例,其它地區請根據實際情況填寫。
    string endpoint = "cn-hangzhou.log.aliyuncs.com";
    // 本樣本從環境變數中擷取AccessKey ID和AccessKey Secret。
    string accessKeyId = (string)getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
    string accessKey = (string)getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
    // Project名稱。
    string project = "";
    // Logstore名稱。
    string logstore = "";
    // 日誌主題。
    string topic = "";
    // Log ServiceClient。
    LOGClient * ptr = new LOGClient(endpoint,accessKeyId,accessKey,LOG_REQUEST_TIMEOUT,"127.0.0.1",false);
    // 構造日誌資料。
    vector<LogItem> logGroup;
    for (int i = 0;i < 1;++i)
    {
        logGroup.push_back(LogItem());
        LogItem & item =  logGroup.back();
        item.timestamp = time(NULL) ;
        item.source="127.0.0.1";
        item.topic=topic;
        item.data.push_back(make_pair<string,string>("status","200"));
        item.data.push_back(make_pair<string,string>("latency","126"));
    }
    try{
        // 寫入日誌。
        ptr -> DeleteConsumerGroup(project,logstore,"test-consumer-group");
        uint32_t shardId = 2;
        int32_t beginTime =  time(NULL);
        ptr -> PostLogStoreLogs(project,logstore,topic,logGroup);
        sleep(1);
        ptr -> PostLogStoreLogs(project,logstore,topic,logGroup);
        sleep(1);
        int32_t endTime = time(NULL);
        ptr -> PostLogStoreLogs(project,logstore,topic,logGroup);
        sleep(1);
        ptr -> PostLogStoreLogs(project,logstore,topic,logGroup);
        sleep(1);
        // 查詢日誌。
        GetCursorResponse beginCursorResp = ptr->GetCursor(project, logstore, shardId, beginTime);
        GetCursorResponse endCursorResp = ptr->GetCursor(project, logstore, shardId, endTime);
        GetBatchLogResponse getLogResp = ptr->GetBatchLog(project, logstore, shardId, 1000, beginCursorResp.result);
        cout << getLogResp.result.logGroups.size() << "\t" << getLogResp.result.logGroupCount << endl;
        getLogResp = ptr->GetBatchLog(project, logstore, 2, 1000, beginCursorResp.result, endCursorResp.result);
        cout << getLogResp.result.logGroups.size() << "\t" << getLogResp.result.logGroupCount << endl;

        ListConsumerGroupResponse lrs = ptr -> ListConsumerGroup(project, logstore);
        for(int i = 0; i < lrs.consumerGroups.size(); ++i)
            cout<<lrs.consumerGroups[i].GetConsumerGroupName()<<", " <<lrs.consumerGroups[i].GetTimeoutInSec()<<", " <<lrs.consumerGroups[i].GetInOrder()<<endl;
        ptr->UpdateCheckpoint(project, logstore, "hahhah", 0, "V0hBVFRIRlVDSw==");
        ListCheckpointResponse lcps = ptr -> ListCheckpoint(project, logstore, "hahhah");
        for(int i =0; i < lcps.consumerGroupCheckpoints.size(); ++i)
        {
            cout<<lcps.consumerGroupCheckpoints[i].GetShard() <<", " << lcps.consumerGroupCheckpoints[i].GetCheckpoint()<<", "<<lcps.consumerGroupCheckpoints[i].GetUpdateTime()<<endl;
        }
        while(true){
            HeartbeatResponse resp = ptr->ConsumerGroupHeartbeat(project, logstore, "hahhah", "cc1", std::vector<uint32_t>());
            cout<<"cc1, heartbeat: ";
            for(std::vector<uint32_t>::const_iterator it = resp.shards.begin(); it != resp.shards.end(); ++it)
                cout<<*it<<", ";
            cout<<endl;
            resp = ptr->ConsumerGroupHeartbeat(project, logstore, "hahhah", "cc2", std::vector<uint32_t>());
            cout<<"cc2, heartbeat: ";
            for(std::vector<uint32_t>::const_iterator it = resp.shards.begin(); it != resp.shards.end(); ++it)
                cout<<*it<<", ";
            cout<<endl;
            usleep(2 * 1000 * 1000);
        }
    }
    catch(LOGException & e)
    {
        cout<<e.GetErrorCode()<<":"<<e.GetMessage()<<endl;
    }
    delete ptr;
}

更多範例程式碼,請參見Aliyun Log C++ SDK