我知道这个问题很老,但它正在引起一些关注,所以我想它可能值得回答,以防其他人来这里。
用于 Google Cloud Platform 的Stackdriver Logging Client Libraries 不再处于测试阶段,因为它们在不久前就已正式发布。我分享的链接包含与安装和使用它们最相关的文档。
运行pip install --upgrade google-cloud-logging 命令后,您将能够使用您的 GCP 帐户进行身份验证,并使用客户端库。
使用它们就像使用 from google.cloud import logging 之类的命令导入库一样简单,然后实例化一个新客户端(您可以默认使用它,甚至可以传递 项目 ID 和 凭据显式)并最终根据需要使用日志。
您可能还想访问official library documentation,在那里您将找到有关如何使用该库、可用的方法和类以及如何做大部分事情的所有详细信息,其中有很多不言自明的示例,甚至是关于如何与 Stackdriver Logging 交互的不同备选方案之间的比较。
作为一个小例子,我也分享一下如何检索状态比“警告”更严重的五个最新日志的sn-p:
# Import the Google Cloud Python client library
from google.cloud import logging
from google.cloud.logging import DESCENDING
# Instantiate a client
logging_client = logging.Client(project = <PROJECT_ID>)
# Set the filter to apply to the logs, this one retrieves GAE logs from the default service with a severity higher than "warning"
FILTER = 'resource.type:gae_app and resource.labels.module_id:default and severity>=WARNING'
i = 0
# List the entries in DESCENDING order and applying the FILTER
for entry in logging_client.list_entries(order_by=DESCENDING, filter_=FILTER): # API call
print('{} - Severity: {}'.format(entry.timestamp, entry.severity))
if (i >= 5):
break
i += 1
请记住,这只是一个简单的示例,使用 Logging Client Library 可以实现很多事情,因此您应该参考我分享的官方文档页面,以便更深入地了解一切有效。