logging.handlers.SMTPHandler and fileConfig
clp2 at rebertia.com
Mon Oct 11 18:15:05 CEST 2010
On Mon, Oct 11, 2010 at 7:52 AM, pstatham <pstatham at sefas.com> 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 [URL="http://docs.python.org/library/
> logging.html#configuration-file-format"]here[/URL], 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.debug('THIS IS A DEBUG MESSAGE')
> logging.error('THIS IS AN ERROR')
> And here's the config file
> 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
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:
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:
logger = logging.getLogger('email')
logger.debug('THIS IS A DEBUG MESSAGE')
logger.error('THIS IS AN ERROR')
More information about the Python-list