使用 Amazon SDK 描述 CloudWatch Logs 订阅筛选条件 - Amazon CloudWatch Logs
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

使用 Amazon SDK 描述 CloudWatch Logs 订阅筛选条件

以下代码示例说明了如何描述 Amazon CloudWatch Logs 现有的订阅筛选条件。

C++
适用于 C++ 的 SDK

包含所需的文件。

#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(); }
Java
SDK for Java 2.x

public static void describeFilters(CloudWatchLogsClient logs, String logGroup) { try { boolean done = false; String newToken = null; while(!done) { DescribeSubscriptionFiltersResponse response; if (newToken == null) { DescribeSubscriptionFiltersRequest request = DescribeSubscriptionFiltersRequest.builder() .logGroupName(logGroup) .limit(1).build(); response = logs.describeSubscriptionFilters(request); } else { DescribeSubscriptionFiltersRequest request = DescribeSubscriptionFiltersRequest.builder() .nextToken(newToken) .logGroupName(logGroup) .limit(1).build(); response = logs.describeSubscriptionFilters(request); } for(SubscriptionFilter filter : response.subscriptionFilters()) { System.out.printf( "Retrieved filter with name %s, " + "pattern %s " + "and destination arn %s", filter.filterName(), filter.filterPattern(), filter.destinationArn()); System.out.println(""); } if(response.nextToken() == null) { done = true; } else { newToken = response.nextToken(); } } } catch (CloudWatchException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } System.out.printf("Done"); }
JavaScript
SDK for JavaScript V3

在单独的模块中创建客户端并将其导出。

import { CloudWatchLogsClient } from "@aws-sdk/client-cloudwatch-logs"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon CloudWatch Logs service client object. export const cwlClient = new CloudWatchLogsClient({ region: REGION });

导入软件开发工具包和客户端模块,然后调用 API。

// Import required AWS SDK clients and commands for Node.js import { DescribeSubscriptionFiltersCommand } from "@aws-sdk/client-cloudwatch-logs"; import { cwlClient } from "./libs/cloudWatchLogsClient.js"; // Set the parameters export const params = { logGroupName: "GROUP_NAME", //GROUP_NAME limit: 5 }; export const run = async () => { try { const data = await cwlClient.send( new DescribeSubscriptionFiltersCommand(params) ); console.log("Success", data.subscriptionFilters); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; // Uncomment this line to run execution within this file. // run();
SDK for JavaScript V2

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the CloudWatchLogs service object var cwl = new AWS.CloudWatchLogs({apiVersion: '2014-03-28'}); var params = { logGroupName: 'GROUP_NAME', limit: 5 }; cwl.describeSubscriptionFilters(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.subscriptionFilters); } });
Kotlin
SDK for Kotlin
注意

这是适用于预览版中功能的预发行文档。本文档随时可能更改。

suspend fun describeFilters(logGroup: String) { val request = DescribeSubscriptionFiltersRequest { logGroupName = logGroup limit = 1 } CloudWatchLogsClient { region = "us-west-2" }.use { cwlClient -> val response = cwlClient.describeSubscriptionFilters(request) response.subscriptionFilters?.forEach { filter -> println("Retrieved filter with name ${filter.filterName} pattern ${filter.filterPattern} and destination ${filter.destinationArn}" ) } } }

有关 Amazon 软件开发工具包开发人员指南和代码示例的完整列表,请参阅 将 CloudWatch Logs 与 Amazon SDK 结合使用。本主题还包括有关入门的信息以及有关先前的软件开发工具包版本的详细信息。