CloudWatch Logs examples using SDK for C++ - Amazon SDK for C++
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

CloudWatch Logs examples using SDK for C++

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for C++ with CloudWatch Logs.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use DeleteSubscriptionFilter.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Include the required files.

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

Delete the subscription filter.

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; }

The following code example shows how to use DescribeSubscriptionFilters.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Include the required files.

#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>

List the subscription filters.

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(); }

The following code example shows how to use PutSubscriptionFilter.

SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

Include the required files.

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

Create the subscription filter.

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; }