繁体   English   中英

python 何时调用 logging.Handler 刷新方法?

[英]python when is logging.Handler flush-method called?

这是我的自定义日志处理程序

import logging
import datetime

from my_app.models import MyModel


class DbLogHandler(logging.Handler): # Inherit from logging.Handler
    def __init__(self):
        # run the regular Handler __init__
        logging.Handler.__init__(self)
        self.entries = []
        print("*****************[DB] INIT db handler")

    def emit(self, record):
        # instantiate the model
        print("@@@@@@@@@@@@@@@@@@@@", self.__hash__())
        print("*****************[DB] called emit on db handler")
        print("*****************[DB] entries has {0:d} entries "
                     "now".format(len(self.entries)))
        # print("*****************[DB] current record is {}".format(record.__dict__))
        try:
            revision_instance = getattr(record, 'revision', None)
            logEntry = MyModel(name=record.name,
                                  log_level_name=record.levelname,
                                  message = record.msg,
                                  module = record.module,
                                  func_name = record.funcName,
                                  line_no = record.lineno,
                                  exception = record.exc_text,
                                  revision = revision_instance
                                  )
            if revision_instance is None:
                return
            self.entries.append(logEntry)
            print("[+] entries has {0:d} entries "
                         "now".format(len(self.entries)))

        except Exception as ex:
            print(ex)
        return

    def flush(self):
        print("************************[*] Flushing {0:d} entries".format(len(self.entries)))
        if self.entries:
            MyModel.objects.bulk_create(self.entries)
            print("************************[+] Successfully flushed {0:d} log entries to "
                         "the DB".format(len(self.entries)))
        else:
            print("************************[*] No log entries for DB logger")

我的日志文件告诉我这个

[2015-09-18 15:12:51,367: WARNING/Worker-2] [+] entries has 3 entries now
[2015-09-18 15:12:51,366: DEBUG/Worker-2] threedi_inpy.tasks.generate_inp_files[None]: {}
[2015-09-18 15:12:51,367: WARNING/Worker-2] @@@@@@@@@@@@@@@@@@@@
[2015-09-18 15:12:51,367: WARNING/Worker-2] 6865789
[2015-09-18 15:12:51,367: WARNING/Worker-2] *****************[DB] called emit on db handler
[2015-09-18 15:12:51,367: WARNING/Worker-2] *****************[DB] entries has 3 entries now
[2015-09-18 15:12:51,367: WARNING/Worker-2] [+] entries has 4 entries now
[2015-09-18 15:12:51,367: INFO/Worker-2] threedi_inpy.tasks.generate_inp_files[None]: [*] Trying to make_sqlite_models_entry for now
[2015-09-18 15:12:51,368: WARNING/Worker-2] @@@@@@@@@@@@@@@@@@@@
[2015-09-18 15:12:51,368: WARNING/Worker-2] 5511201
[2015-09-18 15:12:51,368: WARNING/Worker-2] *****************[DB] called emit on db handler
[2015-09-18 15:12:51,368: WARNING/Worker-2] *****************[DB] entries has 0 entries now
[2015-09-18 15:12:51,368: INFO/Worker-2] threedi_inpy.tasks.generate_inp_files[None]: Skipped directory /srv/var/models/lars_test/test9967b6-52ee-11e5-940c-0050569e25test92/.hg
[2015-09-18 15:12:51,368: WARNING/Worker-2] @@@@@@@@@@@@@@@@@@@@
[2015-09-18 15:12:51,369: WARNING/Worker-2] 5511201
[2015-09-18 15:12:51,369: WARNING/Worker-2] *****************[DB] called emit on db handler
[2015-09-18 15:12:51,369: WARNING/Worker-2] *****************[DB] entries has 0 entries now

flush方法永远不会被调用,因此我的条目将丢失。 因此,我的第一个问题是: flush()何时被调用? 或者,我如何批量写入数据库而不是每次发出?

您基本上是在尝试编写MemoryHandler

它是一个将记录存储在内存中的处理程序,并在达到最大容量或记录某个级别时调用flush 然后flush会将记录发送到target处理程序,该处理程序可能是您的数据库处理程序。

flush确实没有在基本处理程序中调用,它只是在 API 中定义,由子类决定如何以及是否调用它。 它被调用的唯一情况是在调用logging.shutdown时。

例如, MemoryHandler在发出(继承自BufferingHandler )中执行此操作:

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

    Append the record. If shouldFlush() tells us to, call flush() to process
    the buffer.
    """
    self.buffer.append(record)
    if self.shouldFlush(record):
        self.flush()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM