logging module question

chuck cmedcoff at gmail.com
Mon Jan 23 22:18:38 CET 2006


I want to create two different loggers so that users see one output
(e.g. no exception/stack traces) and others (e.g support staff) that
does see stack traces via email.  The code below is close but I still
get console output on the email logger from the "root" logger.  How do
I get rid of it?


import logging
import logging.handlers


def main():
    console = logging.StreamHandler()
    console.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
    console.setLevel(logging.CRITICAL)
    logging.getLogger('').addHandler(console)

    mail = logging.handlers.SMTPHandler('mail.vw.com',
                                       'myaddress at mycompany.com',
                                       'myaddress at mycompany.com',
                                       'Data Processing Error at
location: %s' % "Chuck's Desk")
    mail.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
    logging.getLogger('mail').addHandler(mail)

    try:
        raise Exception("foobar")
    except Exception, e:
        logging.getLogger('').error(e)
        print "Done logging to console"
        logging.getLogger('mail').exception(e)

if __name__ == "__main__":
    main()




More information about the Python-list mailing list