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).
Hello CloudWatch Logs
The following code example shows how to get started using CloudWatch Logs.
- Python
-
- SDK for Python (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
For a complete list of Amazon SDK developer guides and code examples, see
Using CloudWatch Logs with an Amazon SDK.
This topic also includes information about getting started and details about previous SDK versions.