classification
Title: Add a restart option to logging.basicConfig()
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: corona10, michael.kearney, rhettinger, vinay.sajip
Priority: normal Keywords: patch

Created on 2018-06-18 20:39 by rhettinger, last changed 2018-07-11 19:12 by michael.kearney. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7873 merged corona10, 2018-06-23 09:11
Messages (10)
msg319911 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-06-18 20:39
Once a logger or basicConfig() has been called, later calls to basicConfig() are silent ignored.  The makes it very difficult to experiment with or teach the various options at the interactive prompt or in a Jupyter notebook.

What we have:

    >>> import logging
    >>> logging.warning('Too much data')
    WARNING:root:Too much data
    >>> logging.info('Volume is at 80%')
    >>> logging.basicConfig(level=logging.INFO)
    >>> logging.info('Volume is at 80%')
    >>> # Note, no output was created

What we need:

    >>> import logging
    >>> logging.warning('Too much data')
    WARNING:root:Too much data
    >>> logging.info('Volume is at 80%')
    >>> logging.basicConfig(level=logging.INFO, restart=True)
    >>> logging.info('Volume is at 80%')
    INFO:rootVolume is at 80%
msg320196 - (view) Author: Dong-hee Na (corona10) * (Python triager) Date: 2018-06-21 17:33
Can I take a look at this issue?

One question, if we pass the restart keyword as true, should we clear all handlers before we call basicConfig?, or should we maintain it?
msg320218 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2018-06-22 07:39
You would just clear all handlers added to the root logger before calling the existing code:

    try:
        # new code to be added, not tested
        for h in root.handlers[:]:
            root.removeHandler(h)
            h.close()
        # existing code
        if len(root.handlers) == 0:
            handlers = kwargs.pop("handlers", None)

Of course, test case, documentation etc. needs to be added as well.

Not sure if "restart" is the best name, as it implies more than we're doing here. Similarly for "reset". IMO "force" might be better as the keyword argument name. Raymond, what do you think about "force" from a pedagogical point of view?
msg320219 - (view) Author: Dong-hee Na (corona10) * (Python triager) Date: 2018-06-22 08:03
"force" +1
msg320321 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-06-23 19:10
> Raymond, what do you think about "force" from a pedagogical
> point of view?

"force" is also good.  Perhaps "clear_handlers" or "replace_handlers" would be more self-descriptive.
msg320338 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2018-06-23 22:41
> Perhaps "clear_handlers" or "replace_handlers" would be more self-descriptive.

Except that it doesn't *just* clear the handlers - the act of clearing also lets e.g. the level to be set and the formatting to be set by the call.
msg320340 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-06-23 23:57
Not sure what the best word would be (freshen, update, reconfigure, reset, and restart all communicate the intent).  I can't think of a simple word that accurately describes the implementation which removes and closes all root handlers.
msg320355 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2018-06-24 05:42
OK, let's go with "force", then. There are plenty of other places in Python where it's used, so there's that:

https://github.com/python/cpython/search?l=Python&q=force
msg320396 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2018-06-25 04:11
New changeset cf67d6a934b51b1f97e72945b596477b271f70b8 by Vinay Sajip (Dong-hee Na) in branch 'master':
bpo-33897: Add a 'force' keyword argument to logging.basicConfig(). (GH-7873)
https://github.com/python/cpython/commit/cf67d6a934b51b1f97e72945b596477b271f70b8
msg321496 - (view) Author: michael kearney (michael.kearney) * Date: 2018-07-11 19:12
Thank you!
I just stumbled into this problem with logging.basicConfig. In the course of trying to find an acceptable workaround I discovered issue 33897 . I downloaded 3.8.0 and voila my problem is solved.

So, I'll tread lightly, advance to 3.7.0, and keep the latest 3.8.* around for surprises.

Regards to you-all in the development front lines.
-m
Is it acceptable to express appreciation in here?
History
Date User Action Args
2018-07-11 19:12:56michael.kearneysetnosy: + michael.kearney
messages: + msg321496
2018-06-25 04:13:21vinay.sajipsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-06-25 04:11:16vinay.sajipsetmessages: + msg320396
2018-06-24 05:42:50vinay.sajipsetmessages: + msg320355
2018-06-23 23:57:12rhettingersetmessages: + msg320340
2018-06-23 22:41:45vinay.sajipsetmessages: + msg320338
2018-06-23 19:10:06rhettingersetmessages: + msg320321
2018-06-23 09:11:20corona10setkeywords: + patch
stage: patch review
pull_requests: + pull_request7480
2018-06-22 08:03:58corona10setmessages: + msg320219
2018-06-22 07:39:11vinay.sajipsetmessages: + msg320218
2018-06-21 17:33:05corona10setnosy: + corona10
messages: + msg320196
2018-06-18 20:39:17rhettingercreate