我们如何在 azure web 应用程序日志中的 python flask 代码中处理异常

How do we get the exceptions handled in python flask code in azure web app logs

我在 Azure 网络应用程序上有一个 Flask 应用程序 运行。 为了调试的目的,我们在异常处理等地方有打印语句来检查代码的进度。 但是我无法在 azure 日志中找到这些打印语句。

在 VM 上工作时,更容易在控制台上找到日志。

注意:我已尝试使用应用服务日志和诊断日志。但是我无法在 azure 日志中找到自定义打印语句。

我们如何登录 azure 以在 python flask 中包含自定义消息 代码?

try:
    self.asset_name = req_json['assetname']
except KeyError:
    print("Asset Name cannot be empty")
    return "Asset Name cannot be empty"

因此,在对 azure 诊断日志进行了几次尝试之后,我发现使用 python 的日志记录模块会很有用。打印语句确实出现,但不是预期的那样。 所以对我有用的解决方案:

import logging


logger = logging.getLogger(__name__)
logging.basicConfig(format='Custom Logs: %(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')

try:
    self.asset_name = req_json['assetname']
except KeyError:
    logger.exception("Asset Name cannot be empty")
    return "Asset Name cannot be empty"

这些日志可以在我们需要在azure中打开的诊断日志的控制台日志中找到。