簡體   English   中英

傳遞參數以在 python 中創建 LogRecord

[英]Arguments passed to create a LogRecord in python

LogRecord在Python的logging module具有的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調用中收集其他項目? 例如:

  • 路徑名
  • 線諾
  • exc_info
  • 功能
  • sinfo # 這是什么?
  • kwargs # 這只是用戶添加的 kwargs,還是別的什么?

例如,在您的回答中,您能否展示一個對函數調用進行自省以收集上述信息的示例?

幾乎所有這些都發生在Logger.findCaller 中 它檢查當前幀以獲取此信息。

默認 LogRecord 構造函數的 kwargs 參數實際上並未使用。 用戶提供的數據通過extra傳入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM