如何将 python 脚本日志记录到在 Google Cloud VM 上运行的 Google stackdriver 日志记录中

2024-04-24

我正在使用 Google Cloud 虚拟机来运行在 cron 上安排的 python 脚本,我正在寻找某种方法来检查脚本日志并将我的脚本运行日志添加到 google stackdriver 日志记录中

无论如何,是否有将 python 脚本日志添加到 google stackdriver 日志记录中,而不是附加到计算引擎中的日志文件中。此类事情的常用方法是什么?请建议。

          • /usr/bin/python /home/parida_srikanta/test.py>>logs.log 2>&1

BR/ 斯里坎塔


您可以将 Cloud Logging 代理与fluentd https://www.fluentd.org/,这样您就不必更改脚本,并且可以在虚拟机上保留本地日志文件。

主线:

  • 在虚拟机上设置日志代理(手动或通过启动脚本)
  • 设置 fluidd conf 为您的脚本创建专用日志
  • 添加日志记录
  • 通过 Cloud Logging Viewer 检索您的日志

See 官方文档 https://cloud.google.com/logging/docs/agent/installation安装Cloud Logging Agent, and 如何配置 https://cloud.google.com/logging/docs/agent/configuration.

主要步骤:

在您的虚拟机上安装代理:

curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
sudo bash install-logging-agent.sh

为您的新本地添加专用配置fluentd登录进去/etc/google-fluentd/config.d/ :

<source>
    @type tail
    # Format 'none' indicates the log is unstructured (text).
    format none

    # The path of the log file.
    path /tmp/your-script-log.log

    # The path of the position file that records where in the log file
    # we have processed already. This is useful when the agent
    # restarts.
    pos_file /var/lib/google-fluentd/pos/your-script-log.pos

    read_from_head true

    # The log tag for this log input.
    tag your-script-log
</source>

重新启动代理

sudo service google-fluentd restart

写入您的日志文件: echo '测试' >> /tmp/your-script-log.log

您将在里面检索您的日志云日志查看器 https://cloud.google.com/logging/docs/view/

也可以看看我的答案 https://stackoverflow.com/a/58951109/4786620针对不同的问题但具有共同的目标。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将 python 脚本日志记录到在 Google Cloud VM 上运行的 Google stackdriver 日志记录中 的相关文章

随机推荐