python日志记录会刷新每个日志吗? [英] Does python logging flush every log?

查看:194
本文介绍了python日志记录会刷新每个日志吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用标准模块 logging 将日志写入文件时,是否将每个日志分别刷新到磁盘? 例如,下面的代码将刷新10次吗?

logging.basicConfig(level=logging.DEBUG, filename='debug.log')
    for i in xrange(10):
        logging.debug("test")

如果这样,它会变慢吗?

解决方案

是的,它会在每次调用时刷新输出.您可以在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子类,该子类在每次调用emit时都不会执行flush(即使如果发生严重异常/解释器崩溃,也可能会丢失很多日志).

When I write a log to file using the standard module logging, will each log be flushed to disk separately? For example, will the following code flush log by 10 times?

logging.basicConfig(level=logging.DEBUG, filename='debug.log')
    for i in xrange(10):
        logging.debug("test")

if so, will it slow down ?

解决方案

Yes, it does flush the output at every call. You can see this in the source code for the 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)

I wouldn't really mind about the performance of logging, at least not before profiling and discovering that it is a bottleneck. Anyway you can always create a Handler subclass that doesn't perform flush at every call to emit(even though you will risk to lose a lot of logs if a bad exception occurs/the interpreter crashes).

这篇关于python日志记录会刷新每个日志吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆