Python 如何使用logging.BufferingFormatter
Python日志记录教程: 要在批处理中格式化多封邮件,请使用 可以使用BufferingFormatter。除了格式字符串之外 (适用于批处理中的每条消息),有规定 用于标题和尾部格式字符串 以下是如何实现Python 如何使用logging.BufferingFormatter,python,logging,Python,Logging,Python日志记录教程: 要在批处理中格式化多封邮件,请使用 可以使用BufferingFormatter。除了格式字符串之外 (适用于批处理中的每条消息),有规定 用于标题和尾部格式字符串 以下是如何实现BufferingFormatter.format: def format(self, records): """ Format the specified records and return the result as a string. """ rv =
BufferingFormatter.format
:
def format(self, records):
"""
Format the specified records and return the result as a string.
"""
rv = ""
if len(records) > 0:
rv = rv + self.formatHeader(records)
for record in records:
rv = rv + self.linefmt.format(record)
rv = rv + self.formatFooter(records)
return rv
正如我们所看到的,它接受一组记录而不是一条记录,因此每当您尝试将此格式化程序附加到任何处理程序时,都会失败。此外,整个想法假设处理程序具有一些特殊的缓冲行为
因此,我的问题是:这个格式化程序应该如何使用?为了使用相应的
BufferingFormatter
s,您必须使用它。唯一现有的BufferingHandler是MemoryHandler(它是任何其他处理程序的缓冲装饰程序),但它只是将调用重定向到下面的处理程序,并忽略自己的格式化程序。这是否意味着我必须自己编写处理程序?是的。一些谷歌搜索发现了一个例子: