[Python-Dev] Redirecting warnings.showwarning to logging (original) (raw)
Scott David Daniels Scott.Daniels at Acm.Org
Sat Nov 22 23:19:41 CET 2008
- Previous message: [Python-Dev] Redirecting warnings.showwarning to logging
- Next message: [Python-Dev] Redirecting warnings.showwarning to logging
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 Scott.Daniels at Acm.Org
- Previous message: [Python-Dev] Redirecting warnings.showwarning to logging
- Next message: [Python-Dev] Redirecting warnings.showwarning to logging
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]