首页 > 解决方案 > 如何在 aws 云日志中打印异常堆栈跟踪

问题描述

我正在使用 AWS lambda 来执行代码库。

运行 aws lambda 并查看 aws cloudeatch 日志,因此只有最后一个回溯正在打印。

我需要回溯,因为它在本地 python 运行中打印。

class E(Exception):

    def __init__(self, *args, **kwargs):
        for arg in args:
            print(args)
        for kw in kwargs:
            print(kw)


class A:

    def a(self):
        try:
            x = 1 / 0
        except Exception as e:
            raise E("error") from e


def handler(event, context):
    try:
        A().a()
        return True
    except Exception as e:
        raise e


handler(None, None)

输出 :

error
[ERROR] E: error
Traceback (most recent call last):
  File "/var/task/lambda-function.py", line 46, in handler
    raise e
  File "/var/task/lambda-function.py", line 43, in handler
    A().a()
  File "/var/task/lambda-function.py", line 38, in a
    raise E("error") from e

标签: python-3.xexceptionloggingamazon-cloudwatch

解决方案


推荐阅读