all Python 3loggingterm
logging.BufferingFormatter
formatting
A formatter for a batch of records: it joins their lines and adds a header and a footer. Useful when a log is sent as one message.
Syntax
bf = logging.BufferingFormatter(linefmt=None); text = bf.format(records)
Records are joined with no separator: by default format([a, b]) yields 'ab', not two lines, so put the newline into linefmt yourself. The header and footer are empty unless you override formatHeader() and formatFooter() in a subclass. Nothing in logging calls it for you — not even MemoryHandler — so you format the batch by hand.
Examples
import logging
recs = [logging.makeLogRecord({'msg': m}) for m in ('a', 'b')]
print(repr(logging.BufferingFormatter().format(recs))) # → 'ab' — без перевода строки
import logging
class Report(logging.BufferingFormatter):
def formatHeader(self, records):
return f'Записей: {len(records)}\n'
line = logging.Formatter('%(levelname)s %(message)s\n')
recs = [logging.makeLogRecord({'msg': 'диск', 'levelname': 'WARNING'}),
logging.makeLogRecord({'msg': 'сеть', 'levelname': 'ERROR'})]
print(Report(line).format(recs), end='') # → Записей: 2 / WARNING диск / ERROR сеть
See also
Open in the full glossarydocs.python.orgReport an inaccuracy