AWS Lambda Python 3.7 运行时异常日志记录 [英] AWS Lambda Python 3.7 runtime exception logging

查看:45
本文介绍了AWS Lambda Python 3.7 运行时异常日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Python 3.7 运行时引发的未处理异常似乎没有像在 Python 3.6 中那样记录到 CloudWatch.如何在 Python 3.7 中设置记录器来捕获这些信息?

Unhandled exceptions thrown while using the Python 3.7 runtime do not seem to be logged to CloudWatch as they are in Python 3.6. How can you setup the logger in Python 3.7 to capture this information?

也在 AWS 论坛上发布

1.像这样创建一个 lambda 函数:

import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
def lambda_handler(event, context):
logger.info("This shows fine")
raise Exception("I failed")  

2.使用 Python 3.6 运行时运行此函数

START RequestId: a2b6038b-0e5f-11e9-9226-9dfc35a22dcc Version: $LATEST
[INFO]  2019-01-02T07:25:52.797Z    a2b6038b-0e5f-11e9-9226-9dfc35a22dcc //This shows fine
 I failed: Exception
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 9, in lambda_handler
        raise Exception("I failed")
Exception: I failed

END RequestId: a2b6038b-0e5f-11e9-9226-9dfc35a22dcc
REPORT RequestId: a2b6038b-0e5f-11e9-9226-9dfc35a22dcc  Duration: 1.12 ms   Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 21 MB

2.切换到 Python 3.7 运行时并再次运行......没有堆栈跟踪

    START RequestId: 3840aa8e-0e5d-11e9-bece-45a2022a53c6 Version: $LATEST
    [INFO]  2019-01-02T07:08:35.170Z    3840aa8e-0e5d-11e9-bece-45a2022a53c6    This shows fine

    END RequestId: 3840aa8e-0e5d-11e9-bece-45a2022a53c6
    REPORT RequestId: 3840aa8e-0e5d-11e9-bece-45a2022a53c6  Duration: 2.20 ms   Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 20 MB  

推荐答案

是的,我注意到了.为了克服这个问题,我使用了装饰器.

Yep, I've noticed it. To overcome I use a decorator.

def log_errors(func: Callable[[dict, dict], None]):
    def wrapper(*args, **kwargs):
        try:
            func(*args, **kwargs)
        except Exception as err:
            warning(traceback.format_exc())
            raise err

    return wrapper

用法:

@log_errors
def handler(event, context):
...

这篇关于AWS Lambda Python 3.7 运行时异常日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆