如何从GCP上的Stackdriver解析审计日志条目

2024-03-29 09:42:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试在Python中使用Stackdriver Logging Client Libraries检索BigQuery审计日志。在

根据the tutorial,以下代码应该能够获取日志条目:

for entry in client.list_entries():
    do_something_with(entry)

但是,这个迭代器只返回ProtobufEntry,我无法找到如何从这个对象获取实际的日志消息。在

^{pr2}$

上述代码生成以下输出:

$ python log_test.py
<class 'google.cloud.logging.entries.ProtobufEntry'>
<class 'google.cloud.logging.entries.ProtobufEntry'>
<class 'google.cloud.logging.entries.ProtobufEntry'>
....

但是,我找不到任何解析这些对象的方法。在

如何解析实际的日志消息?在


Tags: the对象clientcloud消息librarieslogginggoogle
1条回答
网友
1楼 · 发布于 2024-03-29 09:42:47

ProtobufEntry的字段列在here中。如果有效负载是None,请使用payload_pb,就像我在code中发现的那样。在

以下片段对我有用:

from google.cloud import logging

client = logging.Client()
for entry in client.list_entries():
        timestamp = entry.timestamp.isoformat()
        print('* {}: {}'.format
              (timestamp, entry.payload_pb))

相关问题 更多 >