【问题标题】:Is it possible to inject a log level into structured data in python logging是否可以在 python 日志记录中将日志级别注入结构化数据
【发布时间】: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


    【解决方案1】:

    有很多方法可以做到这一点 - 一种是使用像这样的简单辅助函数(假设 import logging 已经完成):

    def log_structured(logger, level, **kwargs):
        kwargs[level] = level
        # you could also inject the logger name into kwargs here ...
        # convert e.g. string 'INFO' to logging.INFO
        # assumes you only use the standard levels defined in logging
        level = getattr(logging, level)
        logger.log(level, StructuredMessage(**kwargs))
    

    logging.info() 之类的函数使用根记录器,因此您可以执行类似的操作

    root_logger = logging.getLogger()
    log_structured(root_logger, 'INFO', foo='bar', bar='baz', num=123, fnum=123.456)
    

    【讨论】:

      猜你喜欢
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2020-11-19
      相关资源
      最近更新 更多