【问题标题】:Google cloud logging LogEntry fieldsGoogle 云日志记录 LogEntry 字段
【发布时间】:2022-10-20 01:24:41
【问题描述】:

我是 Google 云日志记录的新手。

我使用 Python library,我对 LogEntry 字段有点困惑。这些字段是否意味着由开发人员/代码操作?我正在尝试使用“操作”字段来跟踪具有相同 ID 的操作日志,但是无论我如何尝试格式化日志消息,操作字段仍然在 jsonPayload 中。

我知道,LogEntry 中的“保留”字段将被带到日志的顶层,其余字段将留在 jsonPayload 中。我对么?

我尝试过的众多示例之一:

log_data = {
    "operation": LogEntryOperation(id="string_id", first=True, last=False),
    "my_filed": "my field test",
    "split": {"uid": "ddsadadsad", "index": "dsadadads", "totalSplits": "2"},
}

或者

log_data = {
    "operation": {
        "id": "dsadadsadadsad",
        "producer": "dsadasdcc",
        "first": "true",
        "last": "false",
    },
    "my_filed": "my field test",
    "split": {"uid": "ddsadadsad", "index": "dsadadads", "totalSplits": "2"},
}

我还尝试使用 dict 作为 dict 操作,就像在文档中一样。

我使用 CloudLoggingHandler() 作为处理程序添加到标准 Python 日志记录。然后只是:

logger.info(log_data)

谢谢

【问题讨论】:

    标签: python logging stackdriver google-cloud-logging


    【解决方案1】:

    您可以更新operator,如下所示。这确保了operator 是顶级字段。 log_data 下的字段将始终是jsonPayload 的子字段。

    from google.cloud import logging
    logging_client = logging.Client()
    logger = logging_client.logger("struct_log")
    
    log_data = {
    "my_filed": "my field test",
    "split": {"uid": "ddsadadsad", "index": "dsadadads", "totalSplits": "2"},
    }
    
    logger.log_struct(log_data, operation={"id": "string_id", "first": True, "last": False})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-03
      • 1970-01-01
      • 2015-08-08
      • 2012-06-06
      • 1970-01-01
      • 2023-03-27
      • 2017-08-15
      相关资源
      最近更新 更多