[英]Retrieve list of log names from Google Cloud Stackdriver API with Python
I'm using Google's Stackdriver Logging Client Libraries for Python to programmatically retrieve log entries , similar to using gcloud beta logging read
.我正在使用 Google 的Stackdriver Logging Client Libraries for Python以编程方式检索日志条目,类似于使用
gcloud beta logging read
。
Stackdriver also does provide an API to retrieve a list of log names , which is most probably what gcloud beta logging logs list
uses. Stackdriver 还提供了一个 API 来检索日志名称列表,这很可能是
gcloud beta logging logs list
使用的。
How can I use that API with the Python client libraries?如何将该 API 与 Python 客户端库一起使用? I couldn't find anything in the docs .
我在文档中找不到任何内容。
You can work with the Stackdriver Logging Client Libraries for Python.您可以使用适用于 Python 的Stackdriver Logging 客户端库。 You can install them using the command
pip install --upgrade google-cloud-logging
, and after setting up authentication, you will be able to run a simple program such as the one I have quickly developed and share below.您可以使用命令
pip install --upgrade google-cloud-logging
安装它们,设置身份验证后,您将能够运行一个简单的程序,例如我在下面快速开发和分享的程序。
Before getting into the code itself, let me share with you some interesting documentation pages that will help you develop your own code to retrieve log entries programatically using these Client Libraries:在进入代码本身之前,让我与您分享一些有趣的文档页面,这些页面将帮助您开发自己的代码,以使用这些客户端库以编程方式检索日志条目:
Now that you have all the data you need, let's get into some easy coding:现在您拥有了所需的所有数据,让我们开始一些简单的编码:
# 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
This small snippet imports the Client Libraries, instantiates a client at your project (with Project ID equal to YOUR_PROJECT_ID ), sets a filter that only looks for log entries with a severity higher than WARNING , and finally lists the 6 most recent logs matching the filter.这个小片段导入客户端库,在您的项目中实例化一个客户端(项目 ID 等于YOUR_PROJECT_ID ),设置一个过滤器,只查找严重性高于WARNING 的日志条目,最后列出与过滤器匹配的 6 个最近的日志.
The results of running this code are the following:运行这段代码的结果如下:
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
Which are the entries that exactly correspond to the logs matching the filter I established (note they are shown in inverse order in this screenshot):哪些条目与我建立的过滤器匹配的日志完全对应(请注意,它们在此屏幕截图中以相反的顺序显示):
I hope this small piece of code (accompanied by all the documentation pages I shared) can be useful for you to retrieve logs programmatically using the Stackdriver Client Libraries for Python.我希望这小段代码(随附我共享的所有文档页面)对您使用 Stackdriver Client Libraries for Python 以编程方式检索日志有用。
As pointed out by @otto.poellath, it might also be interesting to list all the log names available in your project.正如@otto.poellath 所指出的,列出项目中所有可用的日志名称也可能很有趣。 However, there is currently not a Python Client Library method available for that purpose, so we will have to work with the old Python API Client Library (not the same as Python Client Library) for that.
但是,目前没有可用于该目的的 Python 客户端库方法,因此我们将不得不使用旧的Python API 客户端库(与 Python 客户端库不同)。 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 ) by providing a library for Python.它可以用命令安装
pip install --upgrade google-api-python-client
,它使更容易使用REST API(如你在你的问题分享其中确实包括以列表日志名称的方法通过提供) Python 库。 It is not as easy to work with it as it is with the new Client Libraries, but it implements all (or almost all) methods that are available through the REST API itself.使用它不像使用新的客户端库那么容易,但它实现了所有(或几乎所有)通过 REST API 本身可用的方法。 Below I share another code snippet that lists all the log names with any written log in your project:
下面我分享另一个代码片段,其中列出了项目中任何书面日志的所有日志名称:
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))
It prints a result such as this one:它打印出这样的结果:
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"
]
}
I know this is not exactly what you ask in the question, as it is not using Python Client Libraries specifically, but I think it might be also interesting for you, knowing that this feature is not available in the new Client Libraries, and the result is similar, as you can access the log names list programmatically using Python.我知道这不是您在问题中问的确切内容,因为它没有专门使用 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)
This is with respect to Cloud Function!这是关于云功能!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.