logging.handlers.SMTPHandler and fileConfig

Discussion in 'Python' started by pstatham, Oct 11, 2010.

  1. pstatham

    pstatham Guest

    I'm trying to use pythons logging.handlers.SMTPHandler with a
    configuration file (so that I don't have to have passwords etc. inside
    of the script)

    Now the guide I'm following is here, now the
    RotatingFileHandler is working, but I never receive an email, or an
    error for the SMTPHandler.

    Anyway here's the python code

    import logging
    import logging.config

    logDir = "./logs/"

    logging.config.fileConfig(logDir+'logging.conf')
    logging.getLogger('email')

    logging.debug('THIS IS A DEBUG MESSAGE')
    logging.error('THIS IS AN ERROR')

    And here's the config file

    [loggers]
    keys=root,email

    [logger_root]
    level=DEBUG
    handlers=rotatingFileHandler

    [logger_email]
    level=ERROR
    handlers=email
    qualname=email

    [formatters]
    keys=emailFormatter,rotatingFormatter

    [formatter_emailFormatter]
    format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s

    [formatter_rotatingFormatter]
    format=%(asctime)s %(name)-12s %(levelname)-8s %(message)s
    datefmt=%m-%d %H:%M

    [handlers]
    keys=email,rotatingFileHandler

    [handler_email]
    class=handlers.SMTPHandler
    level=ERROR
    formatter=emailFormatter
    args=('mail.xx.cocom','',['',],'ERROR!',
    ('user','passwd'))

    [handler_rotatingFileHandler]
    class=handlers.RotatingFileHandler
    level=DEBUG
    formatter=rotatingFormatter
    args=('./logs/log.out', 'maxBytes=1000000', 'backupCount=5')

    Because I wasn't getting an error I decided to temporarily add some
    print statements into .\Lib\logging\handlers.py, In SMTPHandler
    __init__ I print out mailhost, from, to etc. And these are all
    correct. I then inserted a few print statements into the different
    levels of emit to see which branch of the logic it was following. None
    of the print statements print. Which leads me to believe emit() is
    never being called and therefore the email never gets sent.

    So what am I doing wrong?
     
    pstatham, Oct 11, 2010
    #1
    1. Advertising

  2. pstatham

    Chris Rebert Guest

    On Mon, Oct 11, 2010 at 7:52 AM, pstatham <> wrote:
    > I'm trying to use pythons logging.handlers.SMTPHandler with a
    > configuration file (so that I don't have to have passwords etc. inside
    > of the script)
    >
    > Now the guide I'm following is here, now the
    > RotatingFileHandler is working, but I never receive an email, or an
    > error for the SMTPHandler.
    >
    > Anyway here's the python code
    >
    > import logging
    > import logging.config
    >
    > logDir = "./logs/"
    >
    > logging.config.fileConfig(logDir+'logging.conf')
    > logging.getLogger('email')
    >
    > logging.debug('THIS IS A DEBUG MESSAGE')
    > logging.error('THIS IS AN ERROR')
    >
    > And here's the config file

    <snipped>
    > Because I wasn't getting an error I decided to temporarily add some
    > print statements into .\Lib\logging\handlers.py, In SMTPHandler
    > __init__ I print out mailhost, from, to etc. And these are all
    > correct. I then inserted a few print statements into the different
    > levels of emit to see which branch of the logic it was following. None
    > of the print statements print. Which leads me to believe emit() is
    > never being called and therefore the email never gets sent.
    >
    > So what am I doing wrong?


    Based on what the logging docs say about propagation, I /think/ the
    problem is your use of plain logging.debug() and logging.error().
    According to the docs, log messages sent to a child logger (e.g.
    qualname="myapp.foo.bar") are by default propagated to their ancestor
    loggers (e.g. qualname="myapp.foo") hierarchically, until you either
    hit the root logger or a logger for which propagation has been
    disabled.
    logging.debug() and friends send messages directly to the root logger.
    Since propagation works bottom-up rather than top-down, I believe
    these messages aren't propagated to any other loggers, and processing
    stops there, hence why your email logger isn't doing anything; it's
    not receiving any messages in the first place.

    Also, the following line in your code snippet was pointless:
    logging.getLogger('email')
    This returns the logger with the corresponding name, but you don't
    assign it to anything, so the reference gets immediately thrown away.


    I think you need to change your code snippet to the following:
    logging.config.fileConfig(logDir+'logging.conf')
    logger = logging.getLogger('email')

    logger.debug('THIS IS A DEBUG MESSAGE')
    logger.error('THIS IS AN ERROR')

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Oct 11, 2010
    #2
    1. Advertising

  3. Chris Withers, Oct 11, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    498
    Vinay Sajip
    May 1, 2007
  2. Lowell Alleman
    Replies:
    3
    Views:
    543
    Vinay Sajip
    May 29, 2008
  3. akonsu
    Replies:
    4
    Views:
    945
    Chris Withers
    Sep 30, 2009
  4. mynthon
    Replies:
    1
    Views:
    1,714
    Vinay Sajip
    Dec 6, 2009
  5. Herman
    Replies:
    0
    Views:
    181
    Herman
    Feb 19, 2012
Loading...

Share This Page