This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vinay.sajip
Recipients andriusl, peter.otten, vinay.sajip, xtreak
Date 2019-03-07.06:13:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1551939182.06.0.434904530366.issue36193@roundup.psfhosted.org>
In-reply-to
Content
The StreamHandler allows you to specify *exactly* which stream you want to use to log to; sys.stderr is provided as a convenient default argument because that's what a lot of people want a lot of the time.

This is typically done at logging configuration time, or whenever a StreamHandler is created. This is done implicitly by your logging.warning() call (as documented, this calls logging.basicConfig(), which adds a StreamHandler using whatever sys.stderr is set to at the time the StreamHandler is instantiated). Also documented is that basicConfig() is only effective once (it will not do anything if a handler has already been added to the root logger - it is only meant to be use for simple one-off scripts). The documentation for basicConfig() is clear:

"Does basic configuration for the logging system by creating a StreamHandler with a default Formatter and adding it to the root logger. The functions debug(), info(), warning(), error() and critical() will call basicConfig() automatically if no handlers are defined for the root logger.

This function does nothing if the root logger already has handlers configured for it."

If you want to use the real console streams, don't use logging.warning(), but instead explicitly call basicConfig() using __stderr__, as Peter says. Alternatively, use the approach suggested in the cookbook for context-sensitive logging:

https://docs.python.org/3/howto/logging-cookbook.html#using-a-context-manager-for-selective-logging

Closing, as this is not a bug in logging.
History
Date User Action Args
2019-03-07 06:13:02vinay.sajipsetrecipients: + vinay.sajip, peter.otten, xtreak, andriusl
2019-03-07 06:13:02vinay.sajipsetmessageid: <1551939182.06.0.434904530366.issue36193@roundup.psfhosted.org>
2019-03-07 06:13:02vinay.sajiplinkissue36193 messages
2019-03-07 06:13:01vinay.sajipcreate