gpt4 book ai didi

python 什么时候调用 logging.Handler flush-method?

转载 作者:太空宇宙 更新时间:2023-11-04 05:44:55 27 4
gpt4 key购买 nike

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

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 在 emit 中执行此操作(继承自 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()

关于python 什么时候调用 logging.Handler flush-method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32653144/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com