简体   繁体   English

如何从 StackDriver Logging API 反序列化 App Engine 应用程序日志?

[英]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-protosprotobuf 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.

相关问题 Stackdriver日志具有与App Engine背景线程相同的时间戳 - Stackdriver logs have same timestamp for app engine background threads 在App Engine Standard python中使用Google Stackdriver日志时出错 - Error using Google Stackdriver Logging in App Engine Standard python Google Stackdriver使用Redis队列登录App Engine(Python) - Google Stackdriver logging in App Engine (Python) using Redis queue 从开发者控制台读取Google App Engine上的应用程序日志 - Reading Application Logs on Google App Engine from Developer Console 如何有效使用Google App Engine python应用程序中的日志记录? - How to efficiently use logging in Google App Engine python application? 如何通过 python 中的时间戳过滤 GCP 堆栈驱动程序日志 - How to filter GCP stackdriver logs by timestamp from python 如何使用Python日志记录收集App Engine请求的日志? - How do I collect up logs for an App Engine request using Python logging? 如何将日志记录“标签”添加到堆栈驱动程序 - How to add logging 'tags' to stackdriver 将 python 中使用的库的日志发送到谷歌云堆栈驱动程序日志记录 - Send logs of libraries used in python to google cloud stackdriver logging 如何使用OAuth从Python应用程序访问Google App Engine端点API? - How can I access Google App Engine endpoints API from Python application with use OAuth?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM