¿El registro de python limpia todos los registros?


Cuando escribo un registro en un archivo usando el módulo estándar logging, ¿cada registro se descargará al disco por separado? Por ejemplo, ¿el siguiente código se borrará 10 veces?

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

Si es así, ¿disminuirá la velocidad ?

Author: Chris, 2013-05-19

1 answers

Sí, limpia la salida en cada llamada. Puedes ver esto en el código fuente de 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)

Realmente no me importaría el rendimiento del registro, al menos no antes de perfilar y descubrir que es un cuello de botella. De todos modos, siempre puede crear una subclase Handler que no realice flush en cada llamada a emit(aunque se arriesgará a perder muchos registros si se produce una mala excepción/el intérprete se bloquea).

 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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-12-22 08:29:34