logging module question

C

chuck

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',
'(e-mail address removed)',
'(e-mail address removed)',
'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()
 
C

chuck

Ok, so I've figured this out (see below),

mail = logging.handlers.SMTPHandler('mail.vw.com',
'(e-mail address removed)',
'(e-mail address removed)',
'Data Processing Error at
location: %s' % "Chuck's Desk")
mail.setFormatter(logging.Formatter('%(asctime)s %(message)s'))
ml = logging.getLogger('mail')
ml.addHandler(mail)
ml.propagate = 0 ## <---------------do this to turn off
propagation of error reporting

but now I'd like to understand the design rational behind having each
logger in the object hierarchy log the same output by default. Anyone?
 
V

Vinay Sajip

but now I'd like to understand the design rational behind having each
logger in the object hierarchy log the same output by default. Anyone?

Loggers are different to handlers. Loggers map to areas of the
application, handlers map to output destinations. Loggers form a
hierarchy based on names - "myapp" is a parent logger of
"myapp.archiving" or "myapp.workflow", for example. All loggers are
descendants of a root logger.

Logging events are not passed to all loggers, but are by default passed
to the handlers of the logger which logs the event, and then up the
hierarchy to the handlers of ancestor loggers. Both handlers and
loggers can be set to filter by level, or (by using Filters) can filter
according to custom criteria. You can also use custom Formatters to
format the output - for example, a formatter which does not send
traceback information to the output can be set (for a particular
handler).

See PEP 282 for background information on the logging scheme. You can
also look at Apache logging (log4j, log4net etc.) and java.util.logging
which adopt a similar approach (experience having shown that this is a
good approach).

Regards,

Vinay Sajip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top