传递了在python中创建LogRecord的参数

时间:2019-12-13 20:09:52

标签: python logging python-internals

Python logging module中的LogRecord的LogRecord定义为:

class LogRecord(object):
    """
    A LogRecord instance represents an event being logged.
    LogRecord instances are created every time something is logged. They
    contain all the information pertinent to the event being logged. The
    main information passed in is in msg and args, which are combined
    using str(msg) % args to create the message field of the record. The
    record also includes information such as when the record was created,
    the source line where the logging call was made, and any exception
    information to be logged.
    """
    def __init__(self, name, level, pathname, lineno,
                 msg, args, exc_info, func=None, sinfo=None, **kwargs):
        """
        Initialize a logging record with interesting information.
        """
        ct = time.time()
        self.name = name
        self.msg = msg
        # more stuff below

例如,每当有人执行LogRecord动作时创建此logging,例如:

def my_func():
    logging.info('Hello %s', 'Person')
    return 1

在上面的记录器调用中,可以将变量推断为:

name = 'root' # using the root logger since calling `logging.`
level = 'INFO' # or, 20
msg = 'Hello %s'
args = ('Person',)

自省如何从我的logging通话中收集其他物品?例如:

  • 路径名
  • lineno
  • exc_info
  • 功能
  • sinfo#这是什么?
  • kwargs#这是用户添加的kwargs还是其他东西?

例如,在您的答案中,您可以显示一个对函数调用进行自省以收集上述信息的示例吗?

1 个答案:

答案 0 :(得分:0)

几乎所有事件都发生在Logger.findCaller中。它会检查当前帧以获取此信息。

实际上未使用默认LogRecord构造函数的kwargs参数。用户提供的数据通过extra传递。