logging模块SMTPhandler实现日志邮件报警

参数解析如下(API详见Logging handlers):

class logging.handlers.SMTPHandler(mailhostfromaddrtoaddrssubjectcredentials=Nonesecure=Nonetimeout=1.0)

mailhost:指定的服务器,在这里笔者用的是smtp.163.com,port=25,两个参数使用tuple存放('smtp.163.com',25)

fromaddr:即发送人

toaddr:收件人,多个邮箱用list存储['[email protected]','[email protected]']

subject:邮件主题

credentials:凭证需要你的邮箱用户名和SMTP协议密码,可以自己设置,用户名密码也是以tuple形式存储('username','password') 注意:这里的密码不是邮箱登录密码

secure:指定的安全协议,可以不用设置  timeout:发送邮件的时间间隔

[3]代码如下:

errlog = logging.getLogger()
sh = logging.handlers.SMTPHandler(("smtp.163.com", 25), '****@163.com',
                                  ['46*****@qq.com', '*****@163.com'],
                                  "logging from my app",
                                  credentials=('******', '*****'),
                                  )
errlog.addHandler(sh)
try:
    a = 1 / 0
except:
    errlog.warning("hha", exc_info=True)

[4]运行代码

qq和163邮箱分别收到邮件报警,搞定.

转自https://blog.csdn.net/a469357594/article/details/79025234

猜你喜欢

转载自blog.csdn.net/qq_15111861/article/details/81739811