[Python-checkins] bpo-33897: Add a 'force' keyword argument to logging.basicConfig(). (GH-7873)

Vinay Sajip webhook-mailer at python.org
Mon Jun 25 00:11:17 EDT 2018


https://github.com/python/cpython/commit/cf67d6a934b51b1f97e72945b596477b271f70b8
commit: cf67d6a934b51b1f97e72945b596477b271f70b8
branch: master
author: Dong-hee Na <donghee.na92 at gmail.com>
committer: Vinay Sajip <vinay_sajip at yahoo.co.uk>
date: 2018-06-25T05:11:09+01:00
summary:

bpo-33897: Add a 'force' keyword argument to logging.basicConfig(). (GH-7873)

files:
A Misc/NEWS.d/next/Library/2018-06-23-18-09-28.bpo-33897.Hu0yvt.rst
M Doc/library/logging.rst
M Lib/logging/__init__.py
M Lib/test/test_logging.py

diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst
index 774bd83f4103..90fe92b501a0 100644
--- a/Doc/library/logging.rst
+++ b/Doc/library/logging.rst
@@ -1131,7 +1131,7 @@ functions.
    if no handlers are defined for the root logger.
 
    This function does nothing if the root logger already has handlers
-   configured for it.
+   configured, unless the keyword argument *force* is set to ``True``.
 
    .. note:: This function should be called from the main thread
       before other threads are started. In versions of Python prior to
@@ -1183,6 +1183,15 @@ functions.
    |              | with 'filename' or 'stream' - if both are   |
    |              | present, a ``ValueError`` is raised.        |
    +--------------+---------------------------------------------+
+   | ``force``    | If this keyword argument is specified as    |
+   |              | true, any existing handlers attached to the |
+   |              | root logger are removed and closed, before  |
+   |              | carrying out the configuration as specified |
+   |              | by the other arguments.                     |
+   +--------------+---------------------------------------------+
+
+   .. versionchanged:: 3.8
+      The ``force`` argument was added.
 
    .. versionchanged:: 3.2
       The ``style`` argument was added.
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index e6c9f3280e10..29a7d464decf 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -1793,7 +1793,8 @@ def basicConfig(**kwargs):
     Do basic configuration for the logging system.
 
     This function does nothing if the root logger already has handlers
-    configured. It is a convenience method intended for use by simple scripts
+    configured, unless the keyword argument *force* is set to ``True``.
+    It is a convenience method intended for use by simple scripts
     to do one-shot configuration of the logging package.
 
     The default behaviour is to create a StreamHandler which writes to
@@ -1821,13 +1822,19 @@ def basicConfig(**kwargs):
               handlers, which will be added to the root handler. Any handler
               in the list which does not have a formatter assigned will be
               assigned the formatter created in this function.
-
+    force     If this keyword  is specified as true, any existing handlers
+              attached to the root logger are removed and closed, before
+              carrying out the configuration as specified by the other
+              arguments.
     Note that you could specify a stream created using open(filename, mode)
     rather than passing the filename and mode in. However, it should be
     remembered that StreamHandler does not close its stream (since it may be
     using sys.stdout or sys.stderr), whereas FileHandler closes its stream
     when the handler is closed.
 
+    .. versionchanged:: 3.8
+       Added the ``force`` parameter.
+
     .. versionchanged:: 3.2
        Added the ``style`` parameter.
 
@@ -1842,6 +1849,11 @@ def basicConfig(**kwargs):
     # basicConfig() from multiple threads
     _acquireLock()
     try:
+        force = kwargs.pop('force', False)
+        if force:
+            for h in root.handlers[:]:
+                root.removeHandler(h)
+                h.close()
         if len(root.handlers) == 0:
             handlers = kwargs.pop("handlers", None)
             if handlers is None:
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index ba70b117d1e6..b3618bd2ad99 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -3901,6 +3901,27 @@ def test_handlers(self):
         self.assertIs(handlers[2].formatter, f)
         self.assertIs(handlers[0].formatter, handlers[1].formatter)
 
+    def test_force(self):
+        old_string_io = io.StringIO()
+        new_string_io = io.StringIO()
+        old_handlers = [logging.StreamHandler(old_string_io)]
+        new_handlers = [logging.StreamHandler(new_string_io)]
+        logging.basicConfig(level=logging.WARNING, handlers=old_handlers)
+        logging.warning('warn')
+        logging.info('info')
+        logging.debug('debug')
+        self.assertEqual(len(logging.root.handlers), 1)
+        logging.basicConfig(level=logging.INFO, handlers=new_handlers,
+                            force=True)
+        logging.warning('warn')
+        logging.info('info')
+        logging.debug('debug')
+        self.assertEqual(len(logging.root.handlers), 1)
+        self.assertEqual(old_string_io.getvalue().strip(),
+                         'WARNING:root:warn')
+        self.assertEqual(new_string_io.getvalue().strip(),
+                         'WARNING:root:warn\nINFO:root:info')
+
     def _test_log(self, method, level=None):
         # logging.root has no handlers so basicConfig should be called
         called = []
diff --git a/Misc/NEWS.d/next/Library/2018-06-23-18-09-28.bpo-33897.Hu0yvt.rst b/Misc/NEWS.d/next/Library/2018-06-23-18-09-28.bpo-33897.Hu0yvt.rst
new file mode 100644
index 000000000000..64ec3a166f80
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-06-23-18-09-28.bpo-33897.Hu0yvt.rst
@@ -0,0 +1 @@
+Added a 'force' keyword argument to logging.basicConfig().



More information about the Python-checkins mailing list