-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
Conversation
fad9c50
to
a55dfa1
Compare
Doc/library/logging.rst
Outdated
@@ -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. |
There was a problem hiding this comment.
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
.
Doc/library/logging.rst
Outdated
@@ -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 | |
There was a problem hiding this comment.
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.
Lib/logging/__init__.py
Outdated
@@ -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. |
There was a problem hiding this comment.
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
.
Lib/logging/__init__.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
Lib/test/test_logging.py
Outdated
logging.basicConfig(level=logging.INFO, stream=sys.stdout, | ||
force=True) | ||
logging.info('info') | ||
sys.stdout.seek(0) |
There was a problem hiding this comment.
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(). |
There was a problem hiding this comment.
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().
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 |
Lib/test/test_logging.py
Outdated
with support.captured_stdout() as output: | ||
logging.basicConfig(stream=sys.stdout) | ||
logging.warning('warn') | ||
logging.info('info') |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
e6891de
to
5e40306
Compare
@vsajip @pablogsal |
Lib/test/test_logging.py
Outdated
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) |
There was a problem hiding this comment.
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.
@vsajip Thanks updated! |
https://bugs.python.org/issue33897