C++ SDK 日志记录功能可以收集对 TOS 的各种操作日志,并将日志以文件形式存储在本地。
对于一次接口调用,SDK 会在以下位置打印日志:
以下代码展示了如何开启 C++ SDK 日志记录功能。
#include "TosClientV2.h" #include "../src/utils/LogUtils.h" using namespace VolcengineTos; int main(void){ // logFilePath 需要为预期日志文件打印位置,不支持设置为目录或为空; std::string logFilePath = "Your Log File Path"; // 以下代码展示了 C++ SDK 日志开启功能 LogUtils::SetLogger(logFilePath, "tos-cpp-sdk", LogLevel::LogDebug); // 初始化 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"); // 初始化网络等资源 InitializeClient(); // 创建交互的 client TosClientV2 client(region, accessKey, secretKey); // 使用过程中调整打印级别 LogUtils::SetLoggerLevel(LogLevel::LogOff); // 释放网络等资源 CloseClient(); return 0; }
同时 CMakelist 文件中需要新增以下语句。
// CMakeList 文件中增加以下语句 target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/sdk/src/external)
[2022-12-07 22:59:31.811] [tos-cpp-sdk] [debug] canonicalRequest: PUT / date:20221207T145931Z host:sdktest-bucket-1670425171810.tos-cn-chongqing-sdv.volces.com x-tos-acl:private x-tos-date:20221207T145931Z x-tos-storage-class:STANDARD date;host;x-tos-acl;x-tos-date;x-tos-storage-class e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 [2022-12-07 22:59:31.812] [tos-cpp-sdk] [debug] string to sign: TOS4-HMAC-SHA256 20221207T145931Z 20221207/cn-chongqing-sdv/tos/request d67b2d6fc6aa98c269bf4ecaa963605fda4bc3286f550764b79dd61b32c9da74 [2022-12-07 22:59:31.998] [tos-cpp-sdk] [debug] Method:PUT, Host:sdktest-bucket-1670425171810.tos-cn-chongqing-sdv.volces.com, request uri:/, DNS resolution time:59 ms, TCP establish connection time:100 ms, TLS handshake time:0 ms, start transfer time:100 ms, Data sending time:85 ms, Total HTTP request time:185 ms [2022-12-07 22:59:31.998] [tos-cpp-sdk] [info] Response StatusCode:200, RequestId:703f4890aa5428476390aa54-ac101de5-1p2vtU-CB-ccs-front-mtaz-c, Cost:186.078084 ms