]> granicus.if.org Git - python/commitdiff
bpo-37742: Return the root logger when logging.getLogger('root') is c… (#15077)
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Fri, 2 Aug 2019 15:53:00 +0000 (16:53 +0100)
committerGitHub <noreply@github.com>
Fri, 2 Aug 2019 15:53:00 +0000 (16:53 +0100)
* 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.

Lib/logging/__init__.py
Lib/test/test_logging.py
Misc/NEWS.d/next/Library/2019-08-02-14-01-25.bpo-37742.f4Xn9S.rst [new file with mode: 0644]

index 645e0b3c3a67a55c6517b80c7d9e927b115f6c4f..62a87a71b1a3bda757212301b734bcfef71346ea 100644 (file)
@@ -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):
     """
index 6507d79742a42a294a940800bb4a40b050f4b714..dca744c59092f4469d21d59606d81bfee8872c47 100644 (file)
@@ -4856,6 +4856,7 @@ class LoggerTest(BaseTest):
         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 (file)
index 0000000..300ced3
--- /dev/null
@@ -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'.