【发布时间】:2020-07-13 16:15:07
【问题描述】:
我正在尝试从我的 python 程序在 stdout 上写一些结构日志。具体来说,我正在尝试编写符合以下内容的 json,这样当它在 GCP 中运行时,它会被堆栈驱动程序拾取并解释为结构化数据:
https://cloud.google.com/run/docs/logging#run_manual_logging-python
查看 python 文档,我可以为我的结构化数据创建一个类,并将其呈现为单行 json 字符串:
https://docs.python.org/2/howto/logging-cookbook.html#implementing-structured-logging
然而,我坚持的是;如何在没有多余的日志调用的情况下将日志级别添加到该结构化数据:
class StructuredMessage(object):
def __init__(self, **kwargs):
self.kwargs = kwargs
def __str__(self):
return json.dumps(self.kwargs)
// Ideally I should have to type level='INFO' here, is there a way to get the logging
// module to do something to insert that information on the StructuredMessage instance?
logging.info(StructuredMessage(level='INFO'))
【问题讨论】:
标签: python logging google-cloud-platform stackdriver