使用 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/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; }
  • GitHub 中查找说明和更多代码。

  • 有关 API 的详细信息,请参阅《Amazon SDK for C++ API 参考》中的 PutSubscriptionFilter

Java
SDK for Java 2.x

public static void putSubFilters(CloudWatchLogsClient cwl, String filter, String pattern, String logGroup, String functionArn) { try { PutSubscriptionFilterRequest request = PutSubscriptionFilterRequest.builder() .filterName(filter) .filterPattern(pattern) .logGroupName(logGroup) .destinationArn(functionArn) .build(); cwl.putSubscriptionFilter(request); System.out.printf( "Successfully created CloudWatch logs subscription filter %s", filter); } catch (CloudWatchLogsException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • GitHub 中查找说明和更多代码。

  • 有关 API 的详细信息,请参阅《Amazon SDK for Java 2.x API 参考》中的 PutSubscriptionFilter

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 { PutSubscriptionFilterCommand, } from "@aws-sdk/client-cloudwatch-logs"; import { cwlClient } from "./libs/cloudWatchLogsClient.js"; // Set the parameters export const params = { destinationArn: "LAMBDA_FUNCTION_ARN", //LAMBDA_FUNCTION_ARN filterName: "FILTER_NAME", //FILTER_NAME filterPattern: "ERROR", logGroupName: "LOG_GROUP", //LOG_GROUP }; export const run = async () => { try { const data = await cwlClient.send(new PutSubscriptionFilterCommand(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 = { destinationArn: 'LAMBDA_FUNCTION_ARN', filterName: 'FILTER_NAME', filterPattern: 'ERROR', logGroupName: 'LOG_GROUP', }; cwl.putSubscriptionFilter(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

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