简体   繁体   English

AWS Glue Python 中缺少日志

[英]Missing Logs in AWS Glue Python

I have inherited a python script that I'm trying to log in Glue.我继承了我正在尝试登录 Glue 的 python 脚本。 Originally it had prints, but they were only sent once job finished, but it was not possible to see the status of the execution in running time.最初它有打印,但仅在作业完成后才发送,但无法在运行时查看执行状态。

I've changed the log system to the cloudwatch one, but apparently it doesn't send the logs in streaming way as the Spark one, according to this .我已将日志系统更改为 cloudwatch 系统,但显然它不会像 Spark 系统那样以流式传输方式发送日志,根据this

I decided to follow what they recommended and use watchtower for this purposes and I have a code like that:我决定遵循他们的建议并为此目的使用了望塔,我有这样的代码:

def initialize_log() -> logging.Logger:
    logger = logging.getLogger(__name__)
    log_format = "[%(name)s] %(asctime)s %(levelname)-8s %(message)s"

    date_format = "%a, %d %b %Y %H:%M:%S %Z"
    log_stream = sys.stdout

    logging.basicConfig(level=logging.INFO, format=log_format, stream=log_stream, datefmt=date_format)
    logger.addHandler(watchtower.CloudWatchLogHandler(log_group='/aws-glue/python-job', stream_name='my_stream_name'))
    return logger

def log(logger, message):
    logger.info(message)
    logger.info(dict(foo="bar", details={}))

However, when I do:但是,当我这样做时:

logger = initialize_log()

log(logger, "Message")

I cannot find this into message in Cloudwatch /aws-glue/python-job or any directory.我在 Cloudwatch /aws-glue/python-job或任何目录中的消息中找不到此消息。

I would like to ask if you know what I may be doing wrong.我想问你是否知道我可能做错了什么。

Thank you in advance先感谢您

Solved with logging library:使用日志库解决:

import logging

def initialize_log() -> logging.Logger:
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)

    handler = logging.StreamHandler(sys.stdout)
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    return logger


def log(message: str):
    logger.info(message)

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

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