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

使用 Amazon SDK 描述 CloudWatch Logs 日志组

以下代码示例说明了如何描述 CloudWatch Logs 日志组。

.NET
Amazon SDK for .NET
注意

在 GitHub 上查看更多内容。查找完整示例,了解如何在 Amazon 代码示例存储库中进行设置和运行。

using System; using System.Threading.Tasks; using Amazon.CloudWatchLogs; using Amazon.CloudWatchLogs.Model; /// <summary> /// Retrieves information about existing Amazon CloudWatch Logs log groups /// and displays the information on the console. The example was created /// using the AWS SDK for .NET version 3.7 and .NET Core 5.0. /// </summary> public class DescribeLogGroups { public static async Task Main() { // Creates a CloudWatch Logs client using the default // user. If you need to work with resources in another // AWS Region than the one defined for the default user, // pass the AWS Region as a parameter to the client constructor. var client = new AmazonCloudWatchLogsClient(); var request = new DescribeLogGroupsRequest { Limit = 5, }; var response = await client.DescribeLogGroupsAsync(request); if (response.LogGroups.Count > 0) { do { response.LogGroups.ForEach(lg => { Console.WriteLine($"{lg.LogGroupName} is associated with the key: {lg.KmsKeyId}."); Console.WriteLine($"Created on: {lg.CreationTime.Date.Date}"); Console.WriteLine($"Date for this group will be stored for: {lg.RetentionInDays} days.\n"); }); } while (response.NextToken is not null); } } }
  • 有关 API 详细信息,请参阅《Amazon SDK for .NET API 参考》中的 DescribeLogGroups

JavaScript
SDK for JavaScript V3
注意

在 GitHub 上查看更多内容。在 Amazon 代码示例存储库 中查找完整示例,了解如何进行设置和运行。

import { DescribeLogGroupsCommand } from "@aws-sdk/client-cloudwatch-logs"; import { client } from "../libs/client.js"; const run = async () => { // This command will return a list of all log groups in your account. const command = new DescribeLogGroupsCommand({}); try { return await client.send(command); } catch (err) { console.error(err); } }; export default run();
  • 有关 API 详细信息,请参阅《Amazon SDK for JavaScript API 参考》中的 DescribeLogGroups

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