[Python-checkins] bpo-37742: Return the root logger when logging.getLogger('root') is c… (#15077)

Vinay Sajip webhook-mailer at python.org
Fri Aug 2 11:53:05 EDT 2019


https://github.com/python/cpython/commit/cb65b3a4f484ce71dcb76a918af98c7015513025
commit: cb65b3a4f484ce71dcb76a918af98c7015513025
branch: master
author: Vinay Sajip <vinay_sajip at yahoo.co.uk>
committer: GitHub <noreply at github.com>
date: 2019-08-02T16:53:00+01:00
summary:

bpo-37742: Return the root logger when logging.getLogger('root') is c… (#15077)

* bpo-37742: Return the root logger when logging.getLogger('root') is called.

* Added type check guard on logger name in logging.getLogger() and refined a test.

files:
A Misc/NEWS.d/next/Library/2019-08-02-14-01-25.bpo-37742.f4Xn9S.rst
M Lib/logging/__init__.py
M Lib/test/test_logging.py

diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 645e0b3c3a67..62a87a71b1a3 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 isinstance(name, str) and name == root.name:
         return root
+    return Logger.manager.getLogger(name)
 
 def critical(msg, *args, **kwargs):
     """
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 6507d79742a4..dca744c59092 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)
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 000000000000..300ced3fec6b
--- /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'.



More information about the Python-checkins mailing list