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.
This commit is contained in:
Vinay Sajip 2019-08-02 16:53:00 +01:00 committed by GitHub
parent 854d0a4b98
commit cb65b3a4f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 3 deletions

View File

@ -2024,10 +2024,9 @@ def getLogger(name=None):
If no name is specified, return the root logger. If no name is specified, return the root logger.
""" """
if name: if not name or isinstance(name, str) and name == root.name:
return Logger.manager.getLogger(name)
else:
return root return root
return Logger.manager.getLogger(name)
def critical(msg, *args, **kwargs): def critical(msg, *args, **kwargs):
""" """

View File

@ -4856,6 +4856,7 @@ class LoggerTest(BaseTest):
self.assertIs(root, logging.root) self.assertIs(root, logging.root)
self.assertIs(root, logging.getLogger(None)) self.assertIs(root, logging.getLogger(None))
self.assertIs(root, logging.getLogger('')) self.assertIs(root, logging.getLogger(''))
self.assertIs(root, logging.getLogger('root'))
self.assertIs(root, logging.getLogger('foo').root) self.assertIs(root, logging.getLogger('foo').root)
self.assertIs(root, logging.getLogger('foo.bar').root) self.assertIs(root, logging.getLogger('foo.bar').root)
self.assertIs(root, logging.getLogger('foo').parent) self.assertIs(root, logging.getLogger('foo').parent)

View File

@ -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'.