Google Stakdrive日志记录:write_log_entries,数组内部有什么内容?

问题描述 投票:0回答:2

我试图从google文档中找出用于以下示例的数据结构:https://github.com/googleapis/google-cloud-python/tree/master/logging#using-the-apihttps://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数组的内容?以及如何包含特定的时间戳元素,而不是默认的收集时间戳?

python logging google-cloud-platform stackdriver google-cloud-stackdriver
2个回答
2
投票

示例条目。您可以使用自定义更改(例如时间戳)对此进行扩展。

{
    "logName": "projects/development/logs/my-test-log",
    "resource": {"type": "global"},
    "severity": "WARNING", 
    "textPayload": "Test entry"
}

这些链接记录了LogEntry的字段,LogEntry是一个对象(Python字典)。

请注意,某些字段本身就是对象:

LogEntry

LogEntry

{
  "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.
}

此链接记录时间戳:

Timestamp

时间戳的字符串格式如下所示(格里高利历):

Range is from 0001-01-01T00:00:00Z to 9999-12-31T23:59:59.999999999Z

0
投票

这可以是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中看到详细信息。

© www.soinside.com 2019 - 2024. All rights reserved.