[Python-Dev] Guidelines for Logging Usage

Garth T Kidd garthk at gmail.com
Wed Oct 6 00:28:13 CEST 2004

It since occurred to me that people might want the standard library to
be the canonical example of Good Python rather than some messy kludge.
:) So, replacing print with appropriate direct calls to logging or
warnings would be more appropriate than hooking print into those
frameworks by futzing around with handles.

Speaking of which: any thoughts on having warnings use logging rather
than print?

I'm showing 224 files in Lib\ without 'test' in the name and with
print statements, though some of those are sensible (like
zipfile.ZipFile.printdir) and some look like internal tests. Only
cookielib, _LWPCookieJar.py and _MozillaCookieJar seem to import the
logging module. So, you've got some real work on your hands updating
everything to use logging.

Cookie, cookielib, distutils, email, filecmp, idlelib, pickle,
plat-mac, posixfile, pydoc, random, reconvert, regsub, shelve,
statcache, tempfile, tzparse, whrandom and xmllib all import warnings.

I'm not sure if it's appropriate to timestamp the root logger going to
console by default; WARNING:module:message is messy enough without a


On Tue, 05 Oct 2004 10:18:18 +0400, Michael P. Dubner
<dubnerm-news at mail.ru> wrote:
> Garth T Kidd wrote:
> >Strikes me that not much work could let someone keep all of their
> >existing print statements more or less intact and use the logging
> >module properly, too:
> >
> >    import logging
> >
> >    class LoggingHandle:
> >        def __init__(self, wrappedmethod):
> >            self.wrappedmethod = wrappedmethod
> >        def write(self, msg):
> >            msg = msg.rstrip()
> >            if msg:
> >                self.wrappedmethod(msg.rstrip())
> >
> >    logging.basicConfig()
> >    log = logging.getLogger('whatever')
> >    stderr = LoggingHandle(log.error)
> >    stdout = LoggingHandle(log.info)
> >
> >    print >> stdout, "This is informational." # logged as INFO
> >    print >> stderr, "This is an error." # guess.
> >
> >Add some basic infrastructure to logging, and you're done. The
> >Extremely Brave could even alter sys.stderr and sys.stdout.
> Main purpose of logging module is not redirection, but filtering capability.

More information about the Python-Dev mailing list