我试图从google文档中找出用于以下示例的数据结构:https://github.com/googleapis/google-cloud-python/tree/master/logging#using-the-api和https://googleapis.github.io/google-cloud-python/latest/logging/index.html?highlight=write_log_entries
from google.cloud import logging_v2
client = logging_v2.LoggingServiceV2Client()
entries = []
response = client.write_log_entries(entries)
如何格式化entries
数组的内容?以及如何包含特定的时间戳元素,而不是默认的收集时间戳?
示例条目。您可以使用自定义更改(例如时间戳)对此进行扩展。
{
"logName": "projects/development/logs/my-test-log",
"resource": {"type": "global"},
"severity": "WARNING",
"textPayload": "Test entry"
}
这些链接记录了LogEntry的字段,LogEntry是一个对象(Python字典)。
请注意,某些字段本身就是对象:
{
"logName": string,
"resource": {
object(MonitoredResource)
},
"timestamp": string,
"receiveTimestamp": string,
"severity": enum(LogSeverity),
"insertId": string,
"httpRequest": {
object(HttpRequest)
},
"labels": {
string: string,
...
},
"metadata": {
object(MonitoredResourceMetadata)
},
"operation": {
object(LogEntryOperation)
},
"trace": string,
"spanId": string,
"traceSampled": boolean,
"sourceLocation": {
object(LogEntrySourceLocation)
},
// Union field payload can be only one of the following:
"protoPayload": {
"@type": string,
field1: ...,
...
},
"textPayload": string,
"jsonPayload": {
object
}
// End of list of possible types for union field payload.
}
此链接记录时间戳:
时间戳的字符串格式如下所示(格里高利历):
Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z
这可以是LogEntry
对象或dict(与LogEntry具有相同的字段)。后者更容易一些。典型用途是:
from google.cloud import logging_v2
client = logging_v2.LoggingServiceV2Client()
# This defines what object is emitting the logs. See https://cloud.google.com/logging/docs/api/v2/resource-list for a list of options
res = {"type": "gce_instance",
"labels": {
"zone": "us-central1-a",
"instance_id": "1235"}}
entries = []
# Add a plain text log entry
logEntry = {"text_payload": "abc YOUR MESSAGE BLAH"}
entries.append(logEntry)
# Add a structured log entry (https://cloud.google.com/logging/docs/structured-logging)
from google.protobuf.struct_pb2 import Struct
s = Struct()
s["key"] = ["value1","value2"]
logEntry = {"json_payload": s}
entries.append(logEntry)
# write a batch of logs to Stackdriver.
response = client.write_log_entries(entries,
log_name='projects/[PROJECT_ID]/logs/[LOG_ID]',
resource=res)
我找不到write_log_entry的格式化文档,但你可以在code comments
中看到详细信息。