使用 Python 从 Google Cloud Stackdriver API 检索日志名称列表

Retrieve list of log names from Google Cloud Stackdriver API with Python

我正在使用 Google 的 Stackdriver Logging Client Libraries for Python to programmatically retrieve log entries,类似于使用 gcloud beta logging read

Stackdriver 也确实为 retrieve a list of log names 提供了 API,这很可能是 gcloud beta logging logs list 使用的。

如何将 API 与 Python 客户端库一起使用?我在 docs.

中找不到任何内容

您可以使用 Python 的 Stackdriver Logging Client Libraries。您可以使用命令 pip install --upgrade google-cloud-logging 安装它们,并在设置身份验证后,您将能够 运行 一个简单的程序,例如我快速开发并在下面分享的程序。

在进入代码本身之前,让我与您分享一些有趣的文档页面,它们将帮助您开发自己的代码以使用这些客户端库以编程方式检索日志条目:

  • 首先,有通用的 Stackdriver Logging Python Client Library 文档。您会在这里找到各种信息:检索、写入和删除日志、导出日志等。
    • 详细而言,您将对如何 retrieve log entries、从单个或多个项目中列出它们以及应用高级过滤器感兴趣。
  • 另请查看 entry class 的定义方式,以便访问您感兴趣的字段(在我的示例中,我只检查 timestamp 严重性 字段)。
  • 可能也有用的 set of examples

现在您已拥有所需的所有数据,让我们开始一些简单的编码:

# 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 = "<YOUR_PROJECT_ID>")

# Set the filter to apply to the logs
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

这个小片段导入客户端库,在您的项目中实例化一个客户端(项目 ID 等于 YOUR_PROJECT_ID),设置一个只查找日志的过滤器严重性高于 WARNING 的条目,最后列出与过滤器匹配的 6 个最新日志。

运行此代码的结果如下:

my-console:python/logs$ python example_log.py
2018-01-25 09:57:51.524603+00:00 - Severity: ERROR
2018-01-25 09:57:44.696807+00:00 - Severity: WARNING
2018-01-25 09:57:44.661957+00:00 - Severity: ERROR
2018-01-25 09:57:37.948483+00:00 - Severity: WARNING
2018-01-25 09:57:19.632910+00:00 - Severity: ERROR
2018-01-25 09:54:39.334199+00:00 - Severity: ERROR

哪些条目与我建立的过滤器匹配的日志完全对应(注意它们在此屏幕截图中以相反的顺序显示):

我希望这段代码(附有我分享的所有文档页面)对您使用 Python.

的 Stackdriver 客户端库以编程方式检索日志有用。

正如@otto.poellath 所指出的,列出项目中所有可用的日志名称可能也很有趣。但是,目前没有可用于此目的的 Python 客户端库方法,因此我们将不得不通过为 Python 提供库来使用旧的 Python API Client Library (not the same as Python Client Library) for that. It can be installed with the command pip install --upgrade google-api-python-client, and it makes easier to use the REST API (which as you shared in your question does indeed include a method to list log names)。它不像使用新的客户端库那样容易使用,但它实现了通过 REST API 本身可用的所有(或几乎所有)方法。下面我分享另一个代码片段,其中列出了您项目中任何写入日志的所有日志名称:

from apiclient.discovery import build
from oauth2client.client import GoogleCredentials
import json

credentials = GoogleCredentials.get_application_default()
service = build('logging', 'v2', credentials=credentials)

# Methods available in: https://developers.google.com/resources/api-libraries/documentation/logging/v2/python/latest/index.html
collection = service.logs()

# Build the request and execute it
request = collection.list(parent='projects/<YOUR_PROJECT_ID>')
res = request.execute()

print(json.dumps(res, sort_keys=True, indent=4))

它打印出这样的结果:

my-console:python/logs$ python list_logs.py
{
    "logNames": [
        "projects/<YOUR_PROJECT_ID>/logs/my-log",
        "projects/<YOUR_PROJECT_ID>/logs/my-test-log",
        "projects/<YOUR_PROJECT_ID>/logs/python",
        "projects/<YOUR_PROJECT_ID>/logs/requests"
    ]
}

我知道这不是你在问题中问的,因为它没有专门使用 Python 客户端库,但我认为你可能也很感兴趣,因为这个功能不可用在新的客户端库中,结果是相似的,因为您可以使用 Python.

以编程方式访问日志名称列表
from google.cloud import logging

# Instantiate a client
logging_client = logging.Client(project="projectID")

# Set the filter to apply to the logs
FILTER = 'resource.labels.function_name="func_name" severity="DEBUG"'

for entry in logging_client.list_entries(filter_=FILTER):  # API call
    print(entry.severity, entry.timestamp, entry.payload)

这是关于云函数的!