我正在尝试通过邮件发送错误消息。下面是我的代码,它发送3-4次相同的错误邮件,并多次写入日志文件。如何解决这个问题?在下面的代码中,我从url中删除了第一个字符以创建错误。
def get_logger(
LOG_FORMAT = '%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
LOG_NAME = '',
LOG_FILE_INFO = 'file.log',
LOG_FILE_ERROR = 'file.err'):
log = logging.getLogger(LOG_NAME)
if not len(log.handlers):
log_formatter = logging.Formatter(LOG_FORMAT)
file_handler_error = logging.FileHandler(LOG_FILE_ERROR, mode='w')
file_handler_error.setFormatter(log_formatter)
file_handler_error.setLevel(logging.ERROR)
log.addHandler(file_handler_error)
log.setLevel(logging.INFO)
#email sending
smtp_handler = logging.SMTPHandler(mailhost=("smtp.gmail.com", 25),
fromaddr="abcd@gmail.com",
toaddrs="xyz@gmail.com",
subject=u"Job App error!",
credentials=('abcd@gmail.com', 'xxxxx'),
secure=())
log.addHandler(smtp_handler)
return log
def main():
try:
url = 'ttp://www.google.com'
response = requests.get(url)
except Exception as e:
logger.error(e.message)
if __name__ == '__main__':
logger = get_logger()
main()