使用CloudWatch Insights查询Python日志中的胶水查询

问题描述 投票:0回答:1

我是CloudWatch Insights的新手,我一直在尝试了解如何使其与Python日志记录一起使用。目前,我在AWS Glue中有一个PySpark/Python ETL查询设置。我在脚本中使用了默认的logging软件包,用于Python。

我已经阅读了文档,但找不到有关如何格式化日志以使其可通过CloudWatch Insights查询的详细信息。理想情况下,我想在日志消息中设置不同的字段,以便通过Insights查询并从中获取值。

这是脚本中的一条记录消息的示例:

import timeit

start = timeit.default_timer()

...run some code

stop = timeit.default_timer()

runtime = stop - start

logging.info('Runtime: {}'.format(runtime))

我想查询自定义字段,例如@Runtime,以显示该列中所有运行时的不同运行时间。与此相关的是,我还希望看到一个简单的Insight查询示例,以便以此为基础。

任何人的帮助都将不胜感激!

amazon-web-services amazon-cloudwatch aws-glue amazon-cloudwatchlogs aws-cloudwatch-log-insights
1个回答
0
投票

与设置简单的记录器相同

下面的简单示例

MSG_FORMAT = '%(asctime)s %(levelname)s %(name)s: %(message)s'
logging.basicConfig(format=MSG_FORMAT)
logger = logging.getLogger('Something')
logger.setLevel(logging.INFO)

然后输入您的代码

start = timeit.default_timer()

...run some code

stop = timeit.default_timer()

runtime = stop - start

logger.info('Runtime: {}'.format(runtime))
© www.soinside.com 2019 - 2024. All rights reserved.