您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python日志记录会刷新每个日志吗?

python日志记录会刷新每个日志吗?

是的,它会在每次调用时刷新输出。您可以在的源代码中看到StreamHandler

def flush(self):
    """
    Flushes the stream.
    """
    self.acquire()
    try:
        if self.stream and hasattr(self.stream, "flush"):
            self.stream.flush()
    finally:
        self.release()

def emit(self, record):
    """
    Emit a record.

    If a formatter is specified, it is used to format the record.
    The record is then written to the stream with a trailing newline.  If
    exception information is present, it is formatted using
    traceback.print_exception and appended to the stream.  If the stream
    has an 'encoding' attribute, it is used to determine how to do the
    output to the stream.
    """
    try:
        msg = self.format(record)
        stream = self.stream
        stream.write(msg)
        stream.write(self.terminator)
        self.flush()   # <---
    except (KeyboardInterrupt, SystemExit): #pragma: no cover
        raise
    except:
        self.handleError(record)

我真的不会介意日志记录的性能,至少在分析和发现它是瓶颈之前不会。无论如何,您始终可以创建一个在每次调用时都Handler不会执行的子类(即使如果发生严重异常/解释器崩溃,也可能会丢失大量日志)。flush``emit

python 2022/1/1 18:46:48 有311人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶