Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅
中国的 Amazon Web Services 服务入门
(PDF)。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
你好 CloudWatch 日志
以下代码示例显示了如何开始使用 L CloudWatch ogs。
- Python
-
- 适用于 Python 的 SDK(Boto3)
-
def hello_cloudwatch_logs() -> None:
"""
Verifies connectivity to CloudWatch Logs by calling DescribeLogGroups
and displaying the first page of log groups with name, ARN, and
creation time.
"""
logs_client = boto3.client("logs")
try:
response = logs_client.describe_log_groups()
log_groups = response.get("logGroups", list())
if not log_groups:
print("No log groups found in this Region for your account.")
else:
print(f"Found {len(log_groups)} log group(s):\n")
for lg in log_groups:
name = lg.get("logGroupName", "N/A")
arn = lg.get("arn", "N/A")
creation_ms = lg.get("creationTime", 0)
creation_dt = datetime.fromtimestamp(
creation_ms / 1000, tz=timezone.utc
)
print(f" Name: {name}")
print(f" ARN: {arn}")
print(f" Created: {creation_dt.strftime('%Y-%m-%d %H:%M:%S UTC')}")
print()
except ClientError as error:
logger.error(
"Failed to describe log groups: %s",
error.response["Error"]["Message"],
)
raise
有关 S Amazon DK 开发者指南和代码示例的完整列表,请参阅将 L CloudWatch ogs 与 Amazon SDK。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。