简体   繁体   English

Python logging.getLogger 在 AWS Glue python shell 作业中不起作用

[英]Python logging.getLogger not working in AWS Glue python shell job

I am trying to set up a logger for my AWS Glue job using Python's logging module.我正在尝试使用 Python 的logging模块为我的 AWS Glue 作业设置logging器。 I have a Glue job with the type as "Python Shell" using Python version 3.我有一个使用 Python 版本 3 的类型为“Python Shell”的 Glue 作业。

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 .如果我在没有任何name情况下实例化记录器,日志记录工作正常,但是如果我给我的记录器一个name ,它不再起作用,并且我收到一个错误消息: Log stream not found

I have the following code in an example Glue job:我在示例 Glue 作业中有以下代码:

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.请注意,我正在根据这篇文章删除处理程序。

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 .如果我使用代码的版本 1 实例化记录器,它会成功运行并且我能够查看日志,以及在CloudWatch查询它们。

If I run Version 2, giving the logger a name, the Glue job still succeeds.如果我运行版本 2,给记录器一个名字,胶水作业仍然成功。 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.

And I am also not able to query these logs in CloudWatch .而且我也无法在CloudWatch查询这些日志。

I have tried running this code locally using python version 3.6.0 , and both versions work.我尝试使用 python 版本3.6.0在本地运行此代码,并且两个版本都可以使用。 Additionally, both versions of this logging code work inside of a Lambda funcvtion.此外,此日志记录代码的两个版本都在 Lambda 函数内工作。 They only fail in Glue.他们只在 Glue 中失败。

This code worked for me:这段代码对我有用:

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")

I had a similar issue but fixed it with a combination of correct roles and looking in the right place in Cloudwatch.我有一个类似的问题,但通过结合正确的角色和在 Cloudwatch 中寻找正确的位置来修复它。 Make sure you're using the GlueServiceRole.确保您使用的是 GlueServiceRole。 Both Steve and your logging code is fine but the place that you are taken in Cloudwatch when you click on the "logs" button in Glue isn't the correct logging folder. Steve 和您的日志记录代码都很好,但是当您单击 Glue 中的“日志”按钮时,您在 Cloudwatch 中所处的位置不是正确的日志记录文件夹。

Go back into log groups then go into /aws-glue/python-jobs/error and that is the where the logger write to, while the stdout writes to the folder /aws-glue/python-jobs/output.返回日志组,然后进入 /aws-glue/python-jobs/error,这是记录器写入的位置,而标准输出写入文件夹 /aws-glue/python-jobs/output。 It's not a very intuitive setup writing the logs to the error logs folder but hey ho, I'm sure there is a way of configuring it to get it to write where expected.将日志写入错误日志文件夹并不是一个非常直观的设置,但是嘿嘿,我确定有一种方法可以配置它以使其写入预期的位置。

You should be able to name the log stream by using the following (replace "logger-name-here" with your desired log stream name):您应该能够使用以下命令命名日志流(将“logger-name-here”替换为您想要的日志流名称):

import logging

MSG_FORMAT = '%(asctime)s %(levelname)s %(name)s: %(message)s'
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
logging.basicConfig(format=MSG_FORMAT, datefmt=DATETIME_FORMAT)
logger = logging.getLogger(<logger-name-here>)

logger.setLevel(logging.INFO)

logger.info("Test log message")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM