繁体   English   中英

将 python 中使用的库的日志发送到谷歌云堆栈驱动程序日志记录

[英]Send logs of libraries used in python to google cloud stackdriver logging

我正在编写一个使用 pandas、numpy 和其他一堆库的 python 脚本。 我将把它部署到谷歌计算引擎实例,脚本将使用 cron 作业按计划运行。 我正在使用 google.cloud.logging 设置日志记录,以便将云日志记录附加到 python 根记录器。 但是,如果 pandas 引发异常,我希望异常转到堆栈驱动程序,这不会发生。 怎么做? 一种解决方法(丑陋)是将 main 方法(名称 == main)放在 try 中,除了 Exception 作为 e 块和在 except 块中,将异常发送到云日志记录并重新引发异常,以便脚本失败。 是否有我缺少的配置可以在不尝试的情况下解决这个问题?

对我来说,也发生过类似的情况。 我不想做一个巨大的尝试/除了使用https://cloud.google.com/error-reporting/docs/setup/python中提供的client.report()方法

所以最后,对我有用的是实现 getSentry 将日志发送到我的电子邮件

https://docs.sentry.io/platforms/python/

先用 pip 安装:

pip install --upgrade sentry-sdk

然后,使用它来捕获异常:

import sentry_sdk

sentry_sdk.init(
    dsn="https://examplePublicKey@o0.ingest.sentry.io/0",

    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for performance monitoring.
    # We recommend adjusting this value in production.
    traces_sample_rate=1.0,
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM