[英]How to deserialize App Engine application logs from StackDriver Logging API?
As part of migrating to Python 3, I need to migrate from logservice to the StackDriver Logging API .作为迁移到 Python 3 的一部分,我需要从logservice迁移到StackDriver Logging API 。 I have google-cloud-logging installed, and I can successfully fetch GAE application logs with eg:
我安装了google-cloud-logging ,并且可以成功获取 GAE 应用程序日志,例如:
>>> from google.cloud.logging_v2 import LoggingServiceV2Client
>>> entries = LoggingServiceV2Client().list_log_entries(('projects/projectname',),
filter_='resource.type="gae_app" AND protoPayload.@type="type.googleapis.com/google.appengine.logging.v1.RequestLog"')
>>> print(next(iter(entries)))
proto_payload {
type_url: "type.googleapis.com/google.appengine.logging.v1.RequestLog"
value: "\n\ts~brid-gy\022\0018\032R5d..."
}
This gets me a LogEntry
with text application logs in the proto_payload.value
field.这让我在
proto_payload.value
字段中获得了一个带有文本应用程序日志的LogEntry
。 How do I deserialize that field?如何反序列化该字段? I've found lots of related mentions in the docs, but nothing pointing me to a
google.appengine.logging.v1.RequestLog
protobuf generated class anywhere that I can use, if that's even the right idea.我在文档中发现了很多相关的提及,但没有任何东西指向我的
google.appengine.logging.v1.RequestLog
protobuf 生成的 class 任何我可以使用的地方,如果这是正确的想法的话。 Has anyone done this?有人做过吗?
Woo.呜。 Finally got this working.
终于得到了这个工作。 I had to generate and use the Python bindings for the
google.appengine.logging.v1.RequestLog
protocol buffer myself, by hand.我必须自己手动为
google.appengine.logging.v1.RequestLog
协议缓冲区生成和使用 Python 绑定。 Here's how.就是这样。
First, I cloned these two repos at head:首先,我首先克隆了这两个存储库:
Then, I generated request_log_pb2.py
from request_log.proto
by running:然后,我通过运行从
request_log.proto
生成request_log_pb2.py
:
protoc -I googleapis/ -I protobuf/src/ --python_out . googleapis/google/appengine/logging/v1/request_log.proto
Finally, I pip installed googleapis-common-protos
and protobuf
.最后,我 pip 安装了
googleapis-common-protos
和protobuf
。 I was then able to deserialize proto_payload
with:然后我能够反序列
proto_payload
:
from google.cloud.logging_v2 import LoggingServiceV2Client
client = LoggingServiceV2Client(...)
log = next(iter(client.list_log_entries(('projects/brid-gy',),
filter_='logName="projects/brid-gy/logs/appengine.googleapis.com%2Frequest_log"')))
import request_log_pb2
pb = request_log_pb2.RequestLog.FromString(log.proto_payload.value)
print(pb)
You can use the LogEntry.to_api_repr()
function to get a JSON version of the LogEntry
.您可以使用
LogEntry.to_api_repr()
function 来获取LogEntry
的 JSON 版本。
>>> from google.cloud.logging import Client
>>> entries = Client().list_entries(filter_="severity:DEBUG")
>>> entry = next(iter(entries))
>>> entry.to_api_repr()
{'logName': 'projects/PROJECT_NAME/logs/cloudfunctions.googleapis.com%2Fcloud-functions'
, 'resource': {'type': 'cloud_function', 'labels': {'region': 'us-central1', 'function_name': 'tes
t', 'project_id': 'PROJECT_NAME'}}, 'labels': {'execution_id': '1zqolde6afmx'}, 'insertI
d': '000000-f629ab40-aeca-4802-a678-d513e605608e', 'severity': 'DEBUG', 'timestamp': '2019-10-24T2
1:55:14.135056Z', 'trace': 'projects/PROJECT_NAME/traces/9c5201c3061d91c2b624abb950838b4
0', 'textPayload': 'Function execution started'}
Do you really want to use the API v2?您真的要使用 API v2 吗?
If not, use from google.cloud import logging
and set os.environ['GOOGLE_CLOUD_DISABLE_GRPC'] = 'true'
- or similar env setting.如果没有,请使用
from google.cloud import logging
并设置os.environ['GOOGLE_CLOUD_DISABLE_GRPC'] = 'true'
- 或类似的 env 设置。
That will effectively return a JSON in payload
instead of payload_pb
这将有效地返回
payload
中的 JSON 而不是payload_pb
You can try to use: - gcloud beta logging read ${LOG_FILTER}您可以尝试使用: - gcloud beta logging read ${LOG_FILTER}
Or或者
You could change the approach for writing python scripts.您可以更改编写 python 脚本的方法。 You could use the python subprocess module to call the gcloud command (keeping in mind that this is part of the beta command group, which can change on updates without warning. Sending the output into a string, and then parsing the entries that way. This, while it would mean you'd have your logs in a python environment (with all the leverage that brings) however, it wouldn't use the google.cloud library.
您可以使用 python子进程模块来调用 gcloud 命令(请记住,这是 beta 命令组的一部分,它可以在没有警告的情况下更改更新。将 output 发送到字符串中,然后以这种方式解析条目。这个,虽然这意味着您将在 python 环境中拥有您的日志(具有所有带来的影响力),但是它不会使用 google.cloud 库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.