]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-145650: Add `logging.{Formatter,Filter}.__repr__` (GH-145652)
authorJack Danger <github@jackdanger.com>
Thu, 26 Mar 2026 12:41:14 +0000 (05:41 -0700)
committerGitHub <noreply@github.com>
Thu, 26 Mar 2026 12:41:14 +0000 (12:41 +0000)
Lib/logging/__init__.py
Lib/test/test_logging.py
Misc/NEWS.d/next/Library/2026-03-08-00-00-00.gh-issue-145650.LgRepr.rst [new file with mode: 0644]

index 39689a57e6ecd605e6a1a12f73d758ca9eca46ac..6eef90ae5cd9143f423a1cf6d6c28b017e7a8b07 100644 (file)
@@ -622,6 +622,9 @@ class Formatter(object):
         self._fmt = self._style._fmt
         self.datefmt = datefmt
 
+    def __repr__(self):
+        return '<%s (%s)>' % (self.__class__.__name__, self._fmt)
+
     default_time_format = '%Y-%m-%d %H:%M:%S'
     default_msec_format = '%s,%03d'
 
@@ -794,6 +797,9 @@ class Filter(object):
         self.name = name
         self.nlen = len(name)
 
+    def __repr__(self):
+        return '<%s (%s)>' % (self.__class__.__name__, self.name)
+
     def filter(self, record):
         """
         Determine if the specified record is to be logged.
index 05dcea6ce0e98a7b8fed7cdc3b7b4fbe84c8db5e..1a76c2173a30117bc9d7932e0d6a0aa9c973d947 100644 (file)
@@ -404,6 +404,20 @@ class BasicFilterTest(BaseTest):
         r = logging.makeLogRecord({'name': 'spam.eggs'})
         self.assertTrue(f.filter(r))
 
+    def test_filter_repr(self):
+        f = logging.Filter('myapp')
+        self.assertEqual(repr(f), '<Filter (myapp)>')
+
+    def test_filter_repr_empty(self):
+        f = logging.Filter()
+        self.assertEqual(repr(f), '<Filter ()>')
+
+    def test_filter_repr_subclass(self):
+        class MyFilter(logging.Filter):
+            pass
+        f = MyFilter('myapp')
+        self.assertEqual(repr(f), '<MyFilter (myapp)>')
+
 #
 #   First, we define our levels. There can be as many as you want - the only
 #     limitations are that they should be integers, the lowest should be > 0 and
@@ -4914,6 +4928,20 @@ class FormatterTest(unittest.TestCase, AssertErrorMessage):
                 # After PR gh-102412, precision (places) increases from 3 to 7
                 self.assertAlmostEqual(relativeCreated, offset_ns / 1e6, places=7)
 
+    def test_formatter_repr(self):
+        f = logging.Formatter('%(message)s')
+        self.assertEqual(repr(f), '<Formatter (%(message)s)>')
+
+    def test_formatter_repr_default(self):
+        f = logging.Formatter()
+        self.assertEqual(repr(f), '<Formatter (%(message)s)>')
+
+    def test_formatter_repr_subclass(self):
+        class MyFormatter(logging.Formatter):
+            pass
+        f = MyFormatter('%(message)s')
+        self.assertEqual(repr(f), '<MyFormatter (%(message)s)>')
+
 
 class TestBufferingFormatter(logging.BufferingFormatter):
     def formatHeader(self, records):
diff --git a/Misc/NEWS.d/next/Library/2026-03-08-00-00-00.gh-issue-145650.LgRepr.rst b/Misc/NEWS.d/next/Library/2026-03-08-00-00-00.gh-issue-145650.LgRepr.rst
new file mode 100644 (file)
index 0000000..243834d
--- /dev/null
@@ -0,0 +1,3 @@
+Add :meth:`~object.__repr__` support to :class:`logging.Formatter` and
+:class:`logging.Filter`, showing the format string and filter name
+respectively.