[Python-checkins] bpo-37258: Not a bug, but added a unit test and updated documentation. (GH-14229) (GH-14231)
Vinay Sajip
webhook-mailer at python.org
Wed Jun 19 09:46:44 EDT 2019
https://github.com/python/cpython/commit/9eb4b2c8a3387ea901dad793e8d5586880a5968e
commit: 9eb4b2c8a3387ea901dad793e8d5586880a5968e
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Vinay Sajip <vinay_sajip at yahoo.co.uk>
date: 2019-06-19T14:46:19+01:00
summary:
bpo-37258: Not a bug, but added a unit test and updated documentation. (GH-14229) (GH-14231)
(cherry picked from commit 015000165373f8db263ef5bc682f02d74e5782ac)
files:
M Doc/library/logging.rst
M Lib/test/test_logging.py
diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst
index 0e65f316735e..73ce9ad1deec 100644
--- a/Doc/library/logging.rst
+++ b/Doc/library/logging.rst
@@ -50,8 +50,8 @@ listed below.
Logger Objects
--------------
-Loggers have the following attributes and methods. Note that Loggers are never
-instantiated directly, but always through the module-level function
+Loggers have the following attributes and methods. Note that Loggers should
+*NEVER* be instantiated directly, but always through the module-level function
``logging.getLogger(name)``. Multiple calls to :func:`getLogger` with the same
name will always return a reference to the same Logger object.
@@ -1191,7 +1191,9 @@ functions.
The class should define :meth:`__init__` such that only a name argument is
required, and the :meth:`__init__` should call :meth:`Logger.__init__`. This
function is typically called before any loggers are instantiated by applications
- which need to use custom logger behavior.
+ which need to use custom logger behavior. After this call, as at any other
+ time, do not instantiate loggers directly using the subclass: continue to use
+ the :func:`logging.getLogger` API to get your loggers.
.. function:: setLogRecordFactory(factory)
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 97c13a4c5214..13393cd8b372 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -3877,6 +3877,37 @@ class MyLogger(logging.Logger):
logging.setLoggerClass(logging.Logger)
self.assertEqual(logging.getLoggerClass(), logging.Logger)
+ def test_subclass_logger_cache(self):
+ # bpo-37258
+ message = []
+
+ class MyLogger(logging.getLoggerClass()):
+ def __init__(self, name='MyLogger', level=logging.NOTSET):
+ super().__init__(name, level)
+ message.append('initialized')
+
+ logging.setLoggerClass(MyLogger)
+ logger = logging.getLogger('just_some_logger')
+ self.assertEqual(message, ['initialized'])
+ stream = io.StringIO()
+ h = logging.StreamHandler(stream)
+ logger.addHandler(h)
+ try:
+ logger.setLevel(logging.DEBUG)
+ logger.debug("hello")
+ self.assertEqual(stream.getvalue().strip(), "hello")
+
+ stream.truncate(0)
+ stream.seek(0)
+
+ logger.setLevel(logging.INFO)
+ logger.debug("hello")
+ self.assertEqual(stream.getvalue(), "")
+ finally:
+ logger.removeHandler(h)
+ h.close()
+ logging.setLoggerClass(logging.Logger)
+
@support.requires_type_collecting
def test_logging_at_shutdown(self):
# Issue #20037
More information about the Python-checkins
mailing list