AWS CloudWatch 是用于监视 AWS 资源和应用程序的个集中式服务。它可以收集各种类型的监视数据,包括度量、日志和事件。以下是它们之间的区别:
import boto3
cloudwatch = boto3.client('cloudwatch')
response = cloudwatch.put_metric_data(
Namespace='MyApplication',
MetricData=[
{
'MetricName': 'RequestCount',
'Dimensions': [
{
'Name': 'RequestType',
'Value': 'All',
},
],
'Unit': 'Count',
'Value': 1.0,
},
],
)
import logging
import boto3
# Create CloudWatch client
cloudwatch = boto3.client('logs')
# Create logger
logger = logging.getLogger('my_app')
logger.setLevel(logging.DEBUG)
# Create handler and set level to DEBUG
handler = logging.StreamHandler()
handler.setLevel(logging.DEBUG)
# Create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
# Add handler to logger
logger.addHandler(handler)
# Log some messages
logger.error('This is an error message')
logger.warning('This is a warning message')
logger.info('This is an informational message')
logger.debug('This is a debug message')
# Describe Log Streams
response = cloudwatch.describe_log_streams(
logGroupName='/aws/