From 5382f85b5e3a0c13006660123b1f1a17fba9c0f2 Mon Sep 17 00:00:00 2001 From: Harmouch101 Date: Thu, 13 May 2021 11:46:41 +0300 Subject: [PATCH] Implemented formatHeader and formatFooter in BufferingFormatter of the Logging package Signed-off-by: Harmouch101 --- Lib/logging/__init__.py | 39 ++++++++++++++++++++++++++++----------- Lib/test/test_logging.py | 22 ++++++++++------------ 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 555f598de7a925..a9d500d97d04b8 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -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 #--------------------------------------------------------------------------- diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index ee00a32026f65e..f726c6990ac936 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -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 = [ @@ -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)(2)]', f.format(self.records)) + f = logging.BufferingFormatter(lf) + self.assertEqual('[(2)(2)]', f.format(self.records, header, footer)) class ExceptionTest(BaseTest): def test_formatting(self):