Skip to content

Commit

Permalink
Implemented formatHeader and formatFooter in BufferingFormatter of th…
Browse files Browse the repository at this point in the history
…e Logging package

Signed-off-by: Harmouch101 <[email protected]>
  • Loading branch information
wiseaidev committed May 14, 2021
1 parent ab383eb commit 5382f85
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
39 changes: 28 additions & 11 deletions Lib/logging/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,28 +713,45 @@ def __init__(self, linefmt=None):
else:
self.linefmt = _defaultFormatter

def formatHeader(self, records):
def formatHeader(self, records, /, header=""):
"""
Return the header string for the specified records.
Format the specified records with a given header and return the result as a string.
"""
return ""
rv = ""
for i, record in enumerate(records):
if i == 0:
rv += header + record.getMessage()
else:
rv += record.getMessage()
return rv

def formatFooter(self, records):
def formatFooter(self, records, /, footer=""):
"""
Return the footer string for the specified records.
Format the specified records with a given footer and return the result as a string.
"""
return ""
rv = ""
for i, record in enumerate(records):
if i == len(records) - 1:
rv += record.getMessage() + footer
else:
rv += record.getMessage()
return rv

def format(self, records):
def format(self, records, /, header="", footer=""):
"""
Format the specified records and return the result as a string.
"""
rv = ""
temp_records = []
if len(records) > 0:
rv = rv + self.formatHeader(records)
for record in records:
rv = rv + self.linefmt.format(record)
rv = rv + self.formatFooter(records)
for i, record in enumerate(records):
if i == 0:
record.msg = self.linefmt.format(record)
record.msg = self.formatHeader([record], header)
else:
record.msg = self.linefmt.format(record)
temp_records.append(record)
rv = self.formatFooter(temp_records, footer)
return rv

#---------------------------------------------------------------------------
Expand Down
22 changes: 10 additions & 12 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -4014,14 +4014,6 @@ class NoMsecFormatter(logging.Formatter):
f.converter = time.gmtime
self.assertEqual(f.formatTime(r), '21/04/1993 08:03:00')


class TestBufferingFormatter(logging.BufferingFormatter):
def formatHeader(self, records):
return '[(%d)' % len(records)

def formatFooter(self, records):
return '(%d)]' % len(records)

class BufferingFormatterTest(unittest.TestCase):
def setUp(self):
self.records = [
Expand All @@ -4031,15 +4023,21 @@ def setUp(self):

def test_default(self):
f = logging.BufferingFormatter()
header = 'header\n'
footer = '\nfooter'
ref = 'header\nonetwo\nfooter'
self.assertEqual('', f.format([]))
self.assertEqual('onetwo', f.format(self.records))
self.assertEqual(ref, f.format(self.records, header, footer))

def test_custom(self):
f = TestBufferingFormatter()
self.assertEqual('[(2)onetwo(2)]', f.format(self.records))
header = '[(%d)' % len(self.records)
footer = '(%d)]' % len(self.records)
f = logging.BufferingFormatter()
self.assertEqual('[(2)onetwo(2)]', f.format(self.records, header, footer))
lf = logging.Formatter('<%(message)s>')
f = TestBufferingFormatter(lf)
self.assertEqual('[(2)<one><two>(2)]', f.format(self.records))
f = logging.BufferingFormatter(lf)
self.assertEqual('[(2)<one><two>(2)]', f.format(self.records, header, footer))

class ExceptionTest(BaseTest):
def test_formatting(self):
Expand Down

0 comments on commit 5382f85

Please sign in to comment.