logging module question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chuck

    #1

    logging module question

    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.handler s


    def main():
    console = logging.StreamH andler()
    console.setForm atter(logging.F ormatter('%(asc time)s %(message)s'))
    console.setLeve l(logging.CRITI CAL)
    logging.getLogg er('').addHandl er(console)

    mail = logging.handler s.SMTPHandler(' mail.vw.com',
    'myaddress@myco mpany.com',
    'myaddress@myco mpany.com',
    'Data Processing Error at
    location: %s' % "Chuck's Desk")
    mail.setFormatt er(logging.Form atter('%(asctim e)s %(message)s'))
    logging.getLogg er('mail').addH andler(mail)

    try:
    raise Exception("foob ar")
    except Exception, e:
    logging.getLogg er('').error(e)
    print "Done logging to console"
    logging.getLogg er('mail').exce ption(e)

    if __name__ == "__main__":
    main()

  • chuck

    #2
    Re: logging module question

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

    mail = logging.handler s.SMTPHandler(' mail.vw.com',
    'charles.medcof f@gedas.com',
    'charles.medcof f@gedas.com',
    'Data Processing Error at
    location: %s' % "Chuck's Desk")
    mail.setFormatt er(logging.Form atter('%(asctim e)s %(message)s'))
    ml = logging.getLogg er('mail')
    ml.addHandler(m ail)
    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?

    Comment

    • Vinay Sajip

      #3
      Re: logging module question

      > but now I'd like to understand the design rational behind having each[color=blue]
      > logger in the object hierarchy log the same output by default. Anyone?[/color]

      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.archivin g" 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.loggi ng
      which adopt a similar approach (experience having shown that this is a
      good approach).

      Regards,

      Vinay Sajip

      Comment

      Working...