Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a restart option to logging.basicConfig() #78078

Closed
rhettinger opened this issue Jun 18, 2018 · 10 comments
Closed

Add a restart option to logging.basicConfig() #78078

rhettinger opened this issue Jun 18, 2018 · 10 comments
Assignees
Labels
3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@rhettinger
Copy link
Contributor

BPO 33897
Nosy @rhettinger, @vsajip, @corona10
PRs
  • bpo-33897: Add a 'force' keyword argument to logging.basicConfig(). #7873
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/vsajip'
    closed_at = <Date 2018-06-25.04:13:21.016>
    created_at = <Date 2018-06-18.20:39:17.776>
    labels = ['3.8', 'type-feature', 'library']
    title = 'Add a restart option to logging.basicConfig()'
    updated_at = <Date 2018-07-11.19:12:56.701>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2018-07-11.19:12:56.701>
    actor = 'michael.kearney'
    assignee = 'vinay.sajip'
    closed = True
    closed_date = <Date 2018-06-25.04:13:21.016>
    closer = 'vinay.sajip'
    components = ['Library (Lib)']
    creation = <Date 2018-06-18.20:39:17.776>
    creator = 'rhettinger'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33897
    keywords = ['patch']
    message_count = 10.0
    messages = ['319911', '320196', '320218', '320219', '320321', '320338', '320340', '320355', '320396', '321496']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'vinay.sajip', 'michael.kearney', 'corona10']
    pr_nums = ['7873']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue33897'
    versions = ['Python 3.8']

    @rhettinger
    Copy link
    Contributor Author

    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%

    @rhettinger rhettinger added the 3.8 only security fixes label Jun 18, 2018
    @rhettinger rhettinger added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jun 18, 2018
    @corona10
    Copy link
    Member

    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?

    @vsajip
    Copy link
    Member

    vsajip commented Jun 22, 2018

    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?

    @corona10
    Copy link
    Member

    "force" +1

    @rhettinger
    Copy link
    Contributor Author

    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.

    @vsajip
    Copy link
    Member

    vsajip commented Jun 23, 2018

    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.

    @rhettinger
    Copy link
    Contributor Author

    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.

    @vsajip
    Copy link
    Member

    vsajip commented Jun 24, 2018

    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

    @vsajip
    Copy link
    Member

    vsajip commented Jun 25, 2018

    New changeset cf67d6a by Vinay Sajip (Dong-hee Na) in branch 'master':
    bpo-33897: Add a 'force' keyword argument to logging.basicConfig(). (GH-7873)
    cf67d6a

    @vsajip vsajip closed this as completed Jun 25, 2018
    @michaelkearney
    Copy link
    Mannequin

    michaelkearney mannequin commented Jul 11, 2018

    Thank you!
    I just stumbled into this problem with logging.basicConfig. In the course of trying to find an acceptable workaround I discovered bpo-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?

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants