如果我忘记在脚本结束之前关闭MemoryHandler
,即使flushOnClose=False
(Python 3.6)也会显示日志消息“debug”。Python 3日志记忆 - MemoryHandler和flushOnClose行为
我做错了什么或者这是预期的行为?无论手柄如何关闭(即当脚本结束时),我都会认为flushOnClose
会被遵守。
import logging.config
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# file handler, triggered by the memory handler
fh = logging.FileHandler('log.txt')
# set the logging level
fh.setLevel(logging.DEBUG)
# capacity is the number of records
mh = logging.handlers.MemoryHandler(5, flushLevel=logging.ERROR, target=fh, flushOnClose=False)
logger.addHandler(mh)
logger.debug('debug')
# mh.close()
对于参数5, flushLevel=logging.ERROR, target=fh, flushOnClose=False
“调试”消息不应该被显示,因为
- 我还没有加入5个消息到队列
- flushOnClose =假,因此当脚本结束有不应该是齐平的
- 调试不会触发从
flushLevel
冲洗
我发现当我使用mh.close()
时,消息不会如预期那样刷新。但是,当脚本结束时没有mh.close()
(注释),单个调试消息似乎被刷新,尽管设置建议不应该。
无论如何,行为是正常的,即使你打开一个文件,你可以决定现在如果最后关闭或不在。为了不丢失信息,Python会关闭文件:-)类似的行为......最后但并非最不重要的是,如果你写入缓冲区然后忽略它,那么当然没有任何东西会被写入。 –