[Python-Dev] Redirecting warnings.showwarning to logging
Brett Cannon has suggested [1] that the logging package should provide an implementation of warnings.showwarning which redirects to logging. Here are my first thoughts about how this might work: A new function, showwarning( message, category, filename, lineno[, file]) will be added to the logging package. To redirect all warnings to the logging system, it will only be necessary to do the following: warnings.showwarning = logging.showwarning The implementation of logging.showwarning will call formatwarning(message, category, filename, lineno) and will log the resulting string to a warnings logger named "stdlib.warnings" (or perhaps "std.warnings") with level logging.WARNING. The optional file argument to logging.showwarning is only for signature compatibility with warnings.showwarning and will be ignored; in order to configure logging of warnings to any particular destination, the logging configuration will need to add appropriate handlers to the warnings logger. The precise format of the logging message will be determined by the logging configuration in effect, i.e. any formatters configured for the handlers attached to the logger. Does this sound like a reasonable approach? Can someone please suggest any improvements, or let me know if I've missed anything? Regards, Vinay Sajip [1] http://bugs.python.org/issue4384 ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Redirecting warnings.showwarning to logging
On Sat, Nov 22, 2008 at 03:12, Vinay Sajip <[EMAIL PROTECTED]> wrote: > Brett Cannon has suggested [1] that the logging package should provide an > implementation of warnings.showwarning which redirects to logging. Here are my > first thoughts about how this might work: > > A new function, showwarning( message, category, filename, lineno[, file]) will > be added to the logging package. To redirect all warnings to the logging > system, > it will only be necessary to do the following: > > warnings.showwarning = logging.showwarning > > The implementation of logging.showwarning will call formatwarning(message, > category, filename, lineno) and will log the resulting string to a warnings > logger named "stdlib.warnings" (or perhaps "std.warnings") with level > logging.WARNING. The optional file argument to logging.showwarning is only for > signature compatibility with warnings.showwarning and will be ignored; in > order > to configure logging of warnings to any particular destination, the logging > configuration will need to add appropriate handlers to the warnings logger. > The > precise format of the logging message will be determined by the logging > configuration in effect, i.e. any formatters configured for the handlers > attached to the logger. > > Does this sound like a reasonable approach? Can someone please suggest any > improvements, or let me know if I've missed anything? > Basically what I had in mind, so it sounds reasonable to me. -Brett ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Redirecting warnings.showwarning to logging
On Sat, Nov 22, 2008 at 9:12 AM, Vinay Sajip <[EMAIL PROTECTED]> wrote: > Brett Cannon has suggested [1] that the logging package should provide an > implementation of warnings.showwarning which redirects to logging. Here are my > first thoughts about how this might work: > > A new function, showwarning( message, category, filename, lineno[, file]) will > be added to the logging package. To redirect all warnings to the logging > system, > it will only be necessary to do the following: > > warnings.showwarning = logging.showwarning > > Does this sound like a reasonable approach? Can someone please suggest any > improvements, or let me know if I've missed anything? I do have one suggestion for improvement: instead of requiring the person to do this monkey patching, add a new 'log' action to the warnings filter as described by PEP 230 [1] (see: 'The Warnings Filter' section). This way, changing the behavior of how warnings are displayed (or not) is kept consistent and documented. [1] http://www.python.org/dev/peps/pep-0230/ -- Sidnei da Silva Enfold Systems http://enfoldsystems.com Fax +1 832 201 8856 Office +1 713 942 2377 Ext 214 Skype zopedc ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Redirecting warnings.showwarning to logging
Vinay Sajip wrote: Brett Cannon has suggested [1] that the logging package should provide an implementation of warnings.showwarning which redirects to logging. Here are my first thoughts about how this might work: A new function, showwarning( message, category, filename, lineno[, file]) will be added to the logging package. To redirect all warnings to the logging system, it will only be necessary to do the following: warnings.showwarning = logging.showwarning The implementation of logging.showwarning will call formatwarning(message, category, filename, lineno) and will log the resulting string to a warnings logger named "stdlib.warnings" (or perhaps "std.warnings") with level logging.WARNING. The optional file argument to logging.showwarning is only for signature compatibility with warnings.showwarning and will be ignored; in order to configure logging of warnings to any particular destination, the logging configuration will need to add appropriate handlers to the warnings logger. The precise format of the logging message will be determined by the logging configuration in effect, i.e. any formatters configured for the handlers attached to the logger. Match the new warning protocol exactly: def showwarning(message, category, filename, lineno, file=None, line=None): ... If the line is not None, use it (which will happen if you pass it along to showwarning). If the line is None and file is not None, use file to get to your line, rather than opening the filename which may not work. and: def formatwarning(message, category, filename, lineno, line=None): ... Having chased problems around in idle code for a bit, I can assure you that more code than you suspect calls warnings.formatwarning and warnings.showwarning. If you get it wrong, a deprecation warning can prevent a module like md5 from loading (for example). --Scott David Daniels [EMAIL PROTECTED] ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Redirecting warnings.showwarning to logging
Scott David Daniels wrote: > > Match the new warning protocol exactly: > def showwarning(message, category, filename, lineno, > file=None, line=None): > ... > If the line is not None, use it (which will happen if you pass it > along to showwarning). > If the line is None and file is not None, use file to get to your > line, rather than opening the filename which may not work. > Sorry, this had no sooner left my terminal than I realized that the file argument is the output target, and nothing to do with fetching source. I'd not put the data into the log if file is provided, because it may be code trying to extract output formatting. I'd do something like this: if file is not None: old_showwarning(message, category, filename, lineno, file, line) return at the top of the code. --Scott David Daniels [EMAIL PROTECTED] ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Redirecting warnings.showwarning to logging
Sidnei da Silva enfoldsystems.com> writes: > I do have one suggestion for improvement: instead of requiring the > person to do this monkey patching, add a new 'log' action to the > warnings filter as described by PEP 230 [1] (see: 'The Warnings > Filter' section). This way, changing the behavior of how warnings are > displayed (or not) is kept consistent and documented. > > [1] http://www.python.org/dev/peps/pep-0230/ > I see from PEP-230 the following under "Rejected Concerns" stated by GvR: "Paul Prescod, Barry Warsaw and Fred Drake have brought up several additional concerns that I feel aren't critical. I address them here (the concerns are paraphrased, not exactly their words):" [other concerns omitted] - Barry: I'd like to add my own warning action. Maybe if `action' could be a callable as well as a string. Then in my IDE, I could set that to "mygui.popupWarningsDialog". Response: For that purpose you would override warnings.showwarning(). [other concerns omitted] So, monkey-patching appears to be the suggested approach. Indeed, ISTM that the showwarning function is there specifically for the purpose of being monkey-patched out. Regards, Vinay Sajip ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
Re: [Python-Dev] Redirecting warnings.showwarning to logging
On Sat, Nov 22, 2008 at 17:53, Vinay Sajip <[EMAIL PROTECTED]> wrote: > Sidnei da Silva enfoldsystems.com> writes: > >> I do have one suggestion for improvement: instead of requiring the >> person to do this monkey patching, add a new 'log' action to the >> warnings filter as described by PEP 230 [1] (see: 'The Warnings >> Filter' section). This way, changing the behavior of how warnings are >> displayed (or not) is kept consistent and documented. >> >> [1] http://www.python.org/dev/peps/pep-0230/ >> > > I see from PEP-230 the following under "Rejected Concerns" stated by GvR: > > "Paul Prescod, Barry Warsaw and Fred Drake have brought up several additional > concerns that I feel aren't critical. I address them here (the concerns are > paraphrased, not exactly their words):" > >[other concerns omitted] >- Barry: I'd like to add my own warning action. Maybe if `action' > could be a callable as well as a string. Then in my IDE, I > could set that to "mygui.popupWarningsDialog". > > Response: For that purpose you would override warnings.showwarning(). >[other concerns omitted] > > So, monkey-patching appears to be the suggested approach. Indeed, ISTM that > the > showwarning function is there specifically for the purpose of being > monkey-patched out. > It is specifically there to be overridden (and as an aside, it was a pain to support in the C port of warnings), so it really isn't monkey-patching. =) -Brett ___ Python-Dev mailing list [email protected] http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com
