![](/img/trans.png)
[英]AWS lambda of python runtime 3.7 using layer of runtime 3.6
[英]AWS Lambda Python 3.7 runtime exception logging
使用Python 3.7运行时抛出的未处理异常似乎没有像在Python 3.6中那样记录到CloudWatch。 如何在Python 3.7中设置记录器来捕获此信息?
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
是的,我注意到了。 为了克服我使用装饰。
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中的错误。 我注意到它在12月中旬并提出了一个问题; 回应包括以下内容:“我想告诉您,这已被确定为一个真正的问题,Lambda团队目前正在努力解决它。但不幸的是,我没有关于何时解决此问题的ETA “。
我的解决方案是恢复到python3.6运行时,直到亚马逊修复它。
这很奇怪,因为Exception是内置函数,它应该适用于3.6和3.7。
作为一种解决方法,我建议制作自定义异常。 这是一个简单的例子。
test.py
class Error(Exception):
pass
class CustomException(Error):
pass
raise CustomException("I failed")
这是运行时的结果。
TK$ python3 test.py
Traceback (most recent call last):
File "test.py", line 7, in <module>
raise CustomException("I failed")
__main__.CustomException: I failed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.