Python logging.getLogger在AWS Glue python shell作业中不起作用 [英] Python logging.getLogger not working in AWS Glue python shell job
问题描述
我正在尝试使用Python的 logging
模块为我的AWS Glue作业设置记录器.我有一个使用 Python 版本 3 的类型为Python Shell"的 Glue 作业.
I am trying to set up a logger for my AWS Glue job using Python's logging
module. I have a Glue job with the type as "Python Shell" using Python version 3.
如果我实例化没有任何 name
的记录器,则记录工作正常,但是如果我给记录器提供 name
,它将不再起作用,并且我收到一条错误消息,提示:找不到日志流
.
Logging works fine if I instantiate the logger without any name
, but if I give my logger a name
, it no longer works, and I get an error which says: Log stream not found
.
在示例胶水作业中,我有以下代码:
I have the following code in an example Glue job:
import sys
import logging
# Version 1 - this works fine
logger = logging.getLogger()
log_format = "[%(asctime)s %(levelname)-8s %(message)s"
# Version 2 - this fails
logger = logging.getLogger(name = "foobar")
log_format = "[%(name)s] %(asctime)s %(levelname)-8s %(message)s"
date_format = "%a, %d %b %Y %H:%M:%S %Z"
log_stream = sys.stdout
if logger.handlers:
for handler in logger.handlers:
logger.removeHandler(handler)
logging.basicConfig(level = logging.INFO, format = log_format, stream =
log_stream, datefmt = date_format)
logger.info("This is a test.")
请注意,我将根据此帖子删除处理程序.
Note that I'm removing the handlers based on this post.
如果我使用代码的版本1实例化记录器,则记录器将成功运行,并且能够查看日志,并在 CloudWatch
中对其进行查询.
If I instantiate the logger using Version 1 of the code, it runs successfully and I am able to view the logs, as well as query them in CloudWatch
.
如果运行版本2,给记录器命名,则Glue作业仍然成功.但是,如果尝试查看日志,则会收到以下错误消息:
If I run Version 2, giving the logger a name, the Glue job still succeeds. However, if I try to view the logs, I get the following error message:
Log stream not found
The log stream jr_f137743545d3d242618ac95d859b9146fd15d15a0aadce64d8f3ba991ffed012 could not be found. Check if it was correctly created and retry.
而且我也无法在 CloudWatch
中查询这些日志.
And I am also not able to query these logs in CloudWatch
.
我尝试使用python版本 3.6.0
在本地运行此代码,并且两个版本都可以工作.此外,此日志记录代码的两个版本都在Lambda函数内部工作.他们只在胶水中失败.
I have tried running this code locally using python version 3.6.0
, and both versions work. Additionally, both versions of this logging code work inside of a Lambda funcvtion. They only fail in Glue.
推荐答案
此代码对我有用:
import sys
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
root.info("check")
这篇关于Python logging.getLogger在AWS Glue python shell作业中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!