logging.handlers.SMTPHandler and fileConfig

P

pstatham

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

Chris Rebert

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
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
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top