[英]stackdriver logging agent not showing logs read from a custom log file in stackdriver logging viewer on Google cloud platform
[英]How to logging python script log into Google stackdriver logging running on a Google Cloud VM
我正在使用 Google Cloud 虛擬機運行我在 cron 上安排的 python 腳本,我正在尋找某種方法來檢查腳本日志並將我的腳本運行日志添加到 google stackdriver 日志記錄中
無論如何將python腳本日志添加到谷歌堆棧驅動程序日志記錄而不是附加到計算引擎的日志文件中。這些事情的常用方法是什么? 請建議。
BR/ 斯里干達
一個可能的解決方案是制作一個腳本,該腳本讀取logs.log
文件並將日志寫入 Stackdirver,假設您的 VM 具有必要的權限。
使用 Compute Engine 虛擬機實例時,將雲平台訪問范圍添加到每個實例。 通過 Google Cloud Console 創建新實例時,您可以在“創建實例”面板的“身份和 API 訪問”部分中執行此操作。 使用 Compute Engine 默認服務帳號或您選擇的其他服務帳號,然后在 Identity and API access 部分中選擇 Allow full access to all Cloud APIs。 無論您選擇哪個服務帳號,請確保它已在 Cloud Console 的 IAM 和管理部分中被授予日志編寫者角色。
例如下面的腳本:
import google.cloud.logging
import logging
client = google.cloud.logging.Client()
client.setup_logging()
text = 'Hello, world!'
logging.warning(text)
您可以在全局資源類型的python日志下找到日志。
您可以將 Cloud Logging 代理與fluentd 結合使用,這樣您就不必更改腳本,並且可以在虛擬機上保留本地日志文件。
請參閱官方文檔以安裝Cloud Logging Agent
,以及如何配置它。
在您的 VM 上安裝代理:
curl -sSO https://dl.google.com/cloudagents/install-logging-agent.sh
sudo bash install-logging-agent.sh
在/etc/google-fluentd/config.d/
為新的本地fluentd
日志添加專用配置:
<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 'Test' >> /tmp/your-script-log.log
您將在Cloud Logging Viewer 中檢索您的日志
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.