From 6ce1c23549332a6130563578c0c1483defb39374 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Fri, 2 Aug 2019 14:02:08 +0100 Subject: [PATCH 1/2] bpo-37742: Return the root logger when logging.getLogger('root') is called. --- Lib/logging/__init__.py | 5 ++--- .../next/Library/2019-08-02-14-01-25.bpo-37742.f4Xn9S.rst | 5 +++++ 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-08-02-14-01-25.bpo-37742.f4Xn9S.rst diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index 645e0b3c3a67a5..ff7c54dcb9c290 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -2024,10 +2024,9 @@ def getLogger(name=None): If no name is specified, return the root logger. """ - if name: - return Logger.manager.getLogger(name) - else: + if not name or name == root.name: return root + return Logger.manager.getLogger(name) def critical(msg, *args, **kwargs): """ diff --git a/Misc/NEWS.d/next/Library/2019-08-02-14-01-25.bpo-37742.f4Xn9S.rst b/Misc/NEWS.d/next/Library/2019-08-02-14-01-25.bpo-37742.f4Xn9S.rst new file mode 100644 index 00000000000000..300ced3fec6b00 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-08-02-14-01-25.bpo-37742.f4Xn9S.rst @@ -0,0 +1,5 @@ +The logging.getLogger() API now returns the root logger when passed the name +'root', whereas previously it returned a non-root logger named 'root'. This +could affect cases where user code explicitly wants a non-root logger named +'root', or instantiates a logger using logging.getLogger(__name__) in some +top-level module called 'root.py'. From fdf0fdaef70403b786abf80476e8e8139e837a4f Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Fri, 2 Aug 2019 15:52:18 +0100 Subject: [PATCH 2/2] Added type check guard on logger name in logging.getLogger() and refined a test. --- Lib/logging/__init__.py | 2 +- Lib/test/test_logging.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index ff7c54dcb9c290..62a87a71b1a3bd 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -2024,7 +2024,7 @@ def getLogger(name=None): If no name is specified, return the root logger. """ - if not name or name == root.name: + if not name or isinstance(name, str) and name == root.name: return root return Logger.manager.getLogger(name) diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index 6507d79742a42a..dca744c59092f4 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -4856,6 +4856,7 @@ def test_root_logger_aliases(self): self.assertIs(root, logging.root) self.assertIs(root, logging.getLogger(None)) self.assertIs(root, logging.getLogger('')) + self.assertIs(root, logging.getLogger('root')) self.assertIs(root, logging.getLogger('foo').root) self.assertIs(root, logging.getLogger('foo.bar').root) self.assertIs(root, logging.getLogger('foo').parent)