You can use Cloud Logging agent with fluentd, in this way you don't have to change your script, and you're able to keep local log files on your VMs.
Main lines :
- Setup logging agent on your VM (manually or via startup-script)
- Setup fluentd conf to create a dedicated log for your scripts
- Add logging
- Retrieve your logs via Cloud Logging Viewer
See Official documentation to install Cloud Logging Agent
, and How to configure it.
Main steps :
Install agent on your VM:
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
sudo bash install-logging-agent.sh
Add a dedicated configuration for your new local fluentd
log inside /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>
Restart the agent
sudo service google-fluentd restart
Write to your log file :
echo 'Test' >> /tmp/your-script-log.log
You will retrieve your log inside Cloud Logging Viewer
See also my answer to a different question but with common objective.