Skip to content

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

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

Merged
merged 1 commit into from
Jun 25, 2018

Conversation

corona10
Copy link
Member

@corona10 corona10 commented Jun 23, 2018

@@ -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 except when a keyword argument *force* is set as true.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... has handlers configured, unless the keyword argument force is set to True.

@@ -1183,6 +1183,14 @@ functions.
| | with 'filename' or 'stream' - if both are |
| | present, a ``ValueError`` is raised. |
+--------------+---------------------------------------------+
| ``force`` | If this keyword argument is specified as |
| | true, basicConfig removes all handlers of |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@@ -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 except when a keyword argument 'force' is set as true.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... has handlers configured, unless the keyword argument force is set to True.

@@ -1821,13 +1822,18 @@ 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, basicConfig removes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

logging.basicConfig(level=logging.INFO, stream=sys.stdout,
force=True)
logging.info('info')
sys.stdout.seek(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this seek() call needed? Doesn't getvalue() get the entire output, anyway?

@@ -0,0 +1 @@
Add a 'force' keyword argument for basicConfig().
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a 'force' keyword argument to logging.basicConfig().

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@corona10 corona10 changed the title bpo-33897: Add a 'force' keyword argument for basicConfig(). bpo-33897: Added a 'force' keyword argument to logging.basicConfig(). Jun 23, 2018
with support.captured_stdout() as output:
logging.basicConfig(stream=sys.stdout)
logging.warning('warn')
logging.info('info')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Can we log on debug on this line? This way is very clear that the INFO:root:info comes after calling logging.basicConfig(level=logging.INFO, stream=sys.stdout, force=True) and not before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Perhaps better yet, we could assert that output.getvalue() contains only the WARNING line after this call, or immediately before the logging.info() call which follows this one.

@corona10 corona10 force-pushed the bpo-33897 branch 2 times, most recently from e6891de to 5e40306 Compare June 24, 2018 09:43
@corona10
Copy link
Member Author

@vsajip @pablogsal
I updated the unit test to detect the change of handler by using StringIO.

new_string_io = io.StringIO()
old_handlers = [logging.StreamHandler(old_string_io)]
new_handlers = [logging.StreamHandler(new_string_io)]
logging.basicConfig(level=logging.WARN, handlers=old_handlers)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use WARNING here, WARN is only around for backward compatibility.

@corona10 corona10 changed the title bpo-33897: Added a 'force' keyword argument to logging.basicConfig(). bpo-33897: Add a 'force' keyword argument to logging.basicConfig(). Jun 24, 2018
@corona10
Copy link
Member Author

@vsajip Thanks updated!

@vsajip vsajip merged commit cf67d6a into python:master Jun 25, 2018
@corona10 corona10 deleted the bpo-33897 branch June 25, 2018 04:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants