Skip to content

Instantly share code, notes, and snippets.

@fanhang64
Created September 3, 2019 09:21
Embed
What would you like to do?
logging源码实现
# logger.info('This is an info message')
# Logger().info() --> ._log() 然后 -> .makeRecord() # makeRecord 生成LogRecord对象,就是一条日志记录的对象
# 然后调用 Logger().handle(record) ---> .callHandlers(record) 里面循环执行 hdlr.handle(record), 这个hdlr 就是MidnightRotatingFileHandler对象,
# 找MidnightRotatingFileHandler的 父类 handle方法的实现,一直找到Handler类里的下面这个
# def handle(self, record):
# """
# Conditionally emit the specified logging record.
# Emission depends on filters which may have been added to the handler.
# Wrap the actual emission of the record with acquisition/release of
# the I/O thread lock. Returns whether the filter passed the record for
# emission.
# """
# rv = self.filter(record)
# if rv:
# self.acquire()
# try:
# self.emit(record)
# finally:
# self.release()
# return rv
#
# 就是调用emit()
#
# 继续找MidnightRotatingFileHandler父类的emit(), 在BaseRotatingHandler()里找到,是下面这样
# def emit(self, record):
# """
# Emit a record.
#
# Output the record to the file, catering for rollover as described
# in doRollover().
# """
# try:
# if self.shouldRollover(record):
# self.doRollover()
# logging.FileHandler.emit(self, record)
# except Exception:
# self.handleError(record)
#
#
# 然后BaseRotatingHandler的emit调用FileHandler的emit 就是打开文件
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment