CloudWatch 用SDK于 C++ 的日志示例 - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

CloudWatch 用SDK于 C++ 的日志示例

以下代码示例向您展示了如何使用 with Logs 来执行操作和实现常见场 CloudWatch 景。 AWS SDK for C++

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例显示了如何使用DeleteSubscriptionFilter

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,了解如何在 AWS 代码示例存储库中进行设置和运行。

包含所需的文件。

#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/logs/CloudWatchLogsClient.h> #include <aws/logs/model/DeleteSubscriptionFilterRequest.h> #include <iostream>

删除订阅筛选条件。

Aws::CloudWatchLogs::CloudWatchLogsClient cwl; Aws::CloudWatchLogs::Model::DeleteSubscriptionFilterRequest request; request.SetFilterName(filter_name); request.SetLogGroupName(log_group); auto outcome = cwl.DeleteSubscriptionFilter(request); if (!outcome.IsSuccess()) { std::cout << "Failed to delete CloudWatch log subscription filter " << filter_name << ": " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully deleted CloudWatch logs subscription " << "filter " << filter_name << std::endl; }

以下代码示例显示了如何使用DescribeSubscriptionFilters

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,了解如何在 AWS 代码示例存储库中进行设置和运行。

包含所需的文件。

#include <aws/core/Aws.h> #include <aws/core/utils/Outcome.h> #include <aws/logs/CloudWatchLogsClient.h> #include <aws/logs/model/DescribeSubscriptionFiltersRequest.h> #include <aws/logs/model/DescribeSubscriptionFiltersResult.h> #include <iostream> #include <iomanip>

列出订阅筛选条件。

Aws::CloudWatchLogs::CloudWatchLogsClient cwl; Aws::CloudWatchLogs::Model::DescribeSubscriptionFiltersRequest request; request.SetLogGroupName(log_group); request.SetLimit(1); bool done = false; bool header = false; while (!done) { auto outcome = cwl.DescribeSubscriptionFilters( request); if (!outcome.IsSuccess()) { std::cout << "Failed to describe CloudWatch subscription filters " << "for log group " << log_group << ": " << outcome.GetError().GetMessage() << std::endl; break; } if (!header) { std::cout << std::left << std::setw(32) << "Name" << std::setw(64) << "FilterPattern" << std::setw(64) << "DestinationArn" << std::endl; header = true; } const auto &filters = outcome.GetResult().GetSubscriptionFilters(); for (const auto &filter : filters) { std::cout << std::left << std::setw(32) << filter.GetFilterName() << std::setw(64) << filter.GetFilterPattern() << std::setw(64) << filter.GetDestinationArn() << std::endl; } const auto &next_token = outcome.GetResult().GetNextToken(); request.SetNextToken(next_token); done = next_token.empty(); }

以下代码示例显示了如何使用PutSubscriptionFilter

SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,了解如何在 AWS 代码示例存储库中进行设置和运行。

包含所需的文件。

#include <aws/core/Aws.h> #include <aws/logs/CloudWatchLogsClient.h> #include <aws/logs/model/PutSubscriptionFilterRequest.h> #include <aws/core/utils/Outcome.h> #include <iostream>

创建订阅筛选条件。

Aws::CloudWatchLogs::CloudWatchLogsClient cwl; Aws::CloudWatchLogs::Model::PutSubscriptionFilterRequest request; request.SetFilterName(filter_name); request.SetFilterPattern(filter_pattern); request.SetLogGroupName(log_group); request.SetDestinationArn(dest_arn); auto outcome = cwl.PutSubscriptionFilter(request); if (!outcome.IsSuccess()) { std::cout << "Failed to create CloudWatch logs subscription filter " << filter_name << ": " << outcome.GetError().GetMessage() << std::endl; } else { std::cout << "Successfully created CloudWatch logs subscription " << "filter " << filter_name << std::endl; }