【问题标题】:Routing Python logging messages to Cloudwatch in AWS Glue在 AWS Glue 中将 Python 日志记录消息路由到 Cloudwatch
【发布时间】:2020-02-22 05:26:04
【问题描述】:

我编写了一个 Python (pyspark) 库,我在我的 AWS Glue 脚本中使用它。 Python 库使用import logger; log = logging.getLogger(__name__); log.info(message) 的常用方法记录日志。

我希望这些日志在我的胶水作业运行时出现在 Cloudwatch 中。如何将这些 Python 日志路由到 Cloudwatch?

【问题讨论】:

    标签: aws-glue


    【解决方案1】:

    您需要在将日志发送到 Cloudwatch 的 python 记录器上设置 Cloudwatch 处理程序。

    一种方法是使用watchtower 提供的Cloudwatch 处理程序,您可以将其作为usual manner 中的zip 文件导入Glue。

    
    import logging
    
    from watchtower import CloudWatchLogHandler
    
    from awsglue.utils import getResolvedOptions
    args = getResolvedOptions(sys.argv, ['JOB_NAME'])
    
    # This will be used to make sure the log stream is prefixed with the job ID
    # meaning it appears when you click 'see logs' in the Glue web UI.
    job_run_id = args['JOB_RUN_ID']
    lsn = f"{job_run_id}_custom"
    
    # Need to set your default region so that boto3 create a log client in the right region
    os.environ["AWS_DEFAULT_REGION"] = "eu-west-1"
    
    cw = CloudWatchLogHandler(log_group="/aws-glue/jobs/logs-v2", stream_name=lsn)
    
    # Demo of how to route logs made by requests (via urllib3) to cloudwatch
    import requests
    rlog = logging.getLogger("urllib3")
    rlog.setLevel(logging.DEBUG)
    
    rlog.handlers = []
    rlog.addHandler(cw)
    
    r  = requests.get('https://www.bbc.co.uk/news')
    
    # The logs generated by urllib3 will now appear in Cloudwatch
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-27
      • 2017-07-05
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 2020-03-19
      相关资源
      最近更新 更多