【发布时间】:2020-03-15 20:43:37
【问题描述】:
在 GCP 中,对于 ubuntu - 启动脚本日志会自动推送到 /var/log/Syslog,如果长时间需要,我们可能会因为日志轮换而错过这些日志。有没有办法将这些日志重定向到另一个日志文件?
我的启动脚本是一个包含多个命令的简单 bash 脚本,无法将单个命令的输出重定向到文件。
【问题讨论】:
标签: google-cloud-platform cloud startup logfile
在 GCP 中,对于 ubuntu - 启动脚本日志会自动推送到 /var/log/Syslog,如果长时间需要,我们可能会因为日志轮换而错过这些日志。有没有办法将这些日志重定向到另一个日志文件?
我的启动脚本是一个包含多个命令的简单 bash 脚本,无法将单个命令的输出重定向到文件。
【问题讨论】:
标签: google-cloud-platform cloud startup logfile
你可以考虑这个解决方案:
startup-script 中的输出重定向到专用
startup-script.log 文件在/tmp 目录中stackdriver logging代理startup-script.log 添加特定配置然后您就可以通过 GCP Stackdriver Logging 控制台(或通过gcloud 命令)浏览您的日志。
Stackdriver Logging 只会将日志保留 30 天。
对于较长的保留期,您可以轻松创建 sink 以将日志导出到 BigQuery 表或 Cloud Storage 存储分区。
查看有关导出日志的官方文档:
示例startup-script.sh的完整代码:
#! /bin/bash
# install gcp logging agent
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
sudo bash install-logging-agent.sh
# setup a configuration for startup-script logs only
cat > /etc/google-fluentd/config.d/startup-script-log.conf <<- EOM
<source>
@type tail
# Format 'none' indicates the log is unstructured (text).
format none
# The path of the log file.
path /tmp/startup-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/startup-script-log.pos
read_from_head true
# The log tag for this log input.
tag startup-script-log
</source>
EOM
# restart logging agent
sudo service google-fluentd restart
# redirect outputs to dedicated startup-script log
exec &>> /tmp/startup-script-log.log
# your startup-script content
# ...
echo "hello the world"
【讨论】: