I'm attempting to use Python's logging
module to send emails containing logs. The problem that I'm having is that each time I write a log entry, an email is sent. How do I queue the log messages and send a single email at the conclusion of the script?
I have a feeling that it's done with the emit()
method, but I can't figure out how to use it.
import logging, logging.handlers
log = logging.getLogger("mylogger")
log.setLevel(logging.DEBUG)
h2 = logging.handlers.SMTPHandler(mailhost='mailserver',
fromaddr='noreply@example.com',
toaddrs=['me@example.com'],
subject='The log',
credentials=('user','pwd'),
secure=None)
h2.setLevel(logging.INFO)
h2.setFormatter(f)
log.addHandler(h2)
log.info("Did something")
log.info("Did something else")
log.info("This would send a third email. :-(")