You need to enable JavaScript to run this app.
导航

管理实时日志(C++ SDK)

最近更新时间2024.02.04 18:31:05

首次发布时间2023.03.15 15:05:53

TOS 支持日志分析功能,支持通过日志服务,检索分析您访问 TOS 过程中产生的访问日志。通过 TOS C++ SDK 您可以设置日志分析功能的相关配置。

设置实时日志配置规则

注意

  • 使用日志分析功能需要您已开通日志服务功能,并已授权 TOS 访问火山引擎日志服务 TLS。
  • 开启日志分析功能后,日志服务会自动创建日志项目及主题存放 TOS 的相关日志。

示例代码

以下代码用于设置桶 examplebucket 的实时日志配置规则。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);

    PutBucketRealTimeLogInput input(bucketName);

    AccessLogConfiguration accessLogConfiguration;
    // 设置为 true 以使用服务端提供的日志 topic
    accessLogConfiguration.setUseServiceTopic(true);
    // 或设置为 false 同时设置日志的 projectID 和 Topic ID
    // accessLogConfiguration.setUseServiceTopic(false);
    // accessLogConfiguration.setTlsProjectId("Your Tls Project Id");
    // accessLogConfiguration.setTlsTopicId("Your Tls Topic Id");

    RealTimeLogConfiguration realTimeLogConfiguration;
    // 设置角色
    realTimeLogConfiguration.setRole("TOSLogArchiveTLSRole");
    // 设置日志规则
    realTimeLogConfiguration.setConfiguration(accessLogConfiguration);
    
    input.setConfiguration(realTimeLogConfiguration);
    auto output = client.putBucketRealTimeLog(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "PutBucketRealTimeLog failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "PutBucketRealTimeLog success." << std::endl;

    // 释放网络等资源
    CloseClient();
    return 0;
}

获取实时日志配置规则

注意

要获取桶的实时日志配置规则,默认您必须为桶所有者。

示例代码

以下代码用于获取桶 examplebucket 的实时日志配置规则。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);
    GetBucketRealTimeLogInput input(bucketName);

    auto output = client.getBucketRealTimeLog(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "GetBucketRealTimeLog failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "GetBucketRealTimeLog success." << std::endl;
    auto config = output.result().getConfiguration();
    std::cout << "role:" << config.getRole() << std::endl;
    std::cout << "is use service topic:" << config.getConfiguration().isUseServiceTopic() << std::endl;
    std::cout << "project id:" << config.getConfiguration().getTlsProjectId() << std::endl;
    std::cout << "tls topic id:" << config.getConfiguration().getTlsTopicId() << std::endl;
    
    // 释放网络等资源
    CloseClient();
    return 0;
}

删除实时日志配置规则

注意

要删除桶的实时日志配置规则,默认您必须为桶所有者。

示例代码

以下代码用于删除桶 examplebucket 的实时日志配置规则。

#include "TosClientV2.h"
using namespace VolcengineTos;

int main(void){
    // 初始化 TOS 账号信息
    // Your Region 填写 Bucket 所在 Region
    std::string region = "Your Region";
    std::string accessKey = std::getenv("TOS_ACCESS_KEY");
    std::string secretKey = std::getenv("TOS_SECRET_KEY");
    // 填写 Bucket 名称,例如 examplebucket
    std::string bucketName = "examplebucket";

    // 初始化网络等资源
    InitializeClient();
    // 创建交互的 client
    TosClientV2 client(region, accessKey, secretKey);
    DeleteBucketRealTimeLogInput input(bucketName);
    auto output = client.deleteBucketRealTimeLog(input);
    if (!output.isSuccess()) {
        // 异常处理
        std::cout << "DeleteBucketRealTimeLog failed." << output.error().String() << std::endl;
        // 释放网络等资源
        CloseClient();
        return -1;
    }
    std::cout << "DeleteBucketRealTimeLog success." << std::endl;
    // 释放网络等资源
    CloseClient();
    return 0;
}

相关文档

关于实时日志配置的更多信息,请参见日志分析