Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.
CloudWatch Ciao Logs
Il seguente esempio di codice mostra come iniziare a utilizzare CloudWatch Logs.
- Python
-
- SDK per 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
Per un elenco completo delle guide per sviluppatori AWS SDK e degli esempi di codice, consulta. Usare Logs con un CloudWatch AWS SDK Questo argomento include anche informazioni su come iniziare e dettagli sulle versioni precedenti dell’SDK.