您可以将 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针对不同的问题但具有共同的目标。