Czy logowanie Pythona spłukuje każdy dziennik?

Kiedy napiszę log do pliku używając standardowego modułu logging , Czy każdy log będzie spłukiwany na dysk osobno? Na przykład, czy poniższy kod spłukuje dziennik o 10 razy?

logging.basicConfig(level=logging.DEBUG, filename='debug.log')
    for i in xrange(10):
        logging.debug("test")
Jeśli tak, to czy zwolni ?
Author: Chris, 2013-05-19

1 answers

Tak, przepuszcza wyjście przy każdym wywołaniu. Możesz to zobaczyć w kodzie źródłowym 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)

Nie miałbym nic przeciwko wydajności logowania, przynajmniej przed profilowaniem i odkryciem, że jest to wąskie gardło. W każdym razie zawsze możesz utworzyć Handler podklasę, która nie wykonuje flush przy każdym wywołaniu emit (nawet jeśli ryzykujesz utratę wielu logów, jeśli wystąpi zły wyjątek/awaria interpretera).

 47
Author: Bakuriu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-22 08:29:34