0

I am using the handlers TimeRotatingFileHandler and the SMTPHandler. I want to send the INFO messages to the file log and the exceptions the email. My setup is below:

import logging
from logging.handlers import TimeRotatingFileHandler
from logging.handlers import SMTPHandler

filelog_handler = TimedRotatingFileHandler(output_file, when='midnight')
filelog_handler.setFormatter(log_formatter)
filelog_handler.setLevel(logging.DEBUG)
logger.addHandler(filelog_handler)


maillog_handler = SMTPHandler(mailhost=("email.address.org", 25),
                                         fromaddr="email@address.org",
                                         toaddrs="email@address.org",
                                         subject="send_err_email.py Message")
maillog_handler.setLevel(logging.INFO)
logger.addHandler(mail_log)

When I try this code it either sends to both file and email or it sends nothing. Can anyone help me pin point what I am doing wrong or if this is even possible. Your help is greatly appreciated.

JBoogie
  • 1
  • 1

1 Answers1

1

You currently have the SMTPHandler level set at INFO, so any event with level INFO or higher will be handled by that handler. You could replace your penultimate line with

maillog_handler.setLevel(logging.ERROR)

so that only the ERROR and CRITICAL events are sent to the email.

samueljsb
  • 41
  • 3