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 (xx's denote passwords and stuff which I've removed for the sake of posting here)
[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=('xx','xx','xx','ERROR!',['xx','xx']) host=xx from=xx to=xx subject=ERROR! [handler_rotatingFileHandler] class=handlers.RotatingFileHandler level=DEBUG formatter=rotatingFormatter args=('./logs/log.out', 'maxBytes=1000000', 'backupCount=5')
Reading the link I posted above it says
The args entry, when eval()uated in the context of the logging package’s namespace, is the list of arguments to the constructor for the handler class. Refer to the constructors for the relevant handlers, or to the examples below, to see how typical entries are constructed.
Now the constructor for the SMTPHandler takes the form
class logging.handlers.SMTPHandler(mailhost, fromaddr, toaddrs, subject[, credentials])¶ Returns a new instance of the SMTPHandler class. The instance is initialized with the from and to addresses and subject line of the email. The toaddrs should be a list of strings. To specify a non-standard SMTP port, use the (host, port) tuple format for the mailhost argument. If you use a string, the standard SMTP port is used. If your SMTP server requires authentication, you can specify a (username, password) tuple for the credentials argument.
But looking at their example for SMTPHandler they have their args SMTPHandler set like so
[handler_hand07] class=handlers.SMTPHandler level=WARN formatter=form07 args=('localhost', 'from@abc', ['user1@abc', 'user2@xyz'], 'Logger Subject')
Note how the args aren't in the same order as the constructor specifies and it's missing the to field, I've tried using the same format as above in my config file but I still receive no email and no errors.
The arguments I've defined for SMTP are correct as I previously had the SMTPHandler directly instantiated in the code and it worked.
Can someone please help me figure out how to get it working using a config file :)