flask发送错误日志到邮箱

来源:互联网 发布:qq影音windows版电脑版 编辑:程序博客网 时间:2024/05/21 08:54
 loging 模块,无疑就是用来记录app在服务器中运行时所发生的错误信息,在flask中也不例外,先看以下代码
1:ErrorMail(将错误信息发送报告到指定邮箱里头,提醒管理员)
ADMINS = ['yourname@example.com']if not app.debug: import logging from logging.handlers import SMTPHandler mail_handler = SMTPHandler('127.0.0.1', 'server-error@example.com', ADMINS, 'YourApplication Failed') mail_handler.setLevel(logging.ERROR) app.logger.addHandler(mail_handler)
在debug模式为‘false’的服务器上,通过引入logging库文件,引入处理类 SMTPHander,当服务器中的app发生了错误报告时,会通过其设置的邮箱地址来将app的后天错误信息发送到指定的
server-error@example.com 邮箱中,提醒管理员后台出现错误信息,及时对错误信息进行处理。

2:logging to a file(将错误信息存储到文件当中去)
其中的属性设置如下:
  i.FileHandler 将错误信息加入到一个文件系统的文件中去,
  ii.RotatingFileHandler 将日志文件中的文本资料进行回滚处理、
  iii.SysLogHandler 将日志信息发送到Unix系统文件中去
  iV.NTEventLogHandler 将日志信息发送到windows系统文件中去
日志文件代码实例:
if not app.debug: import logging from themodule import TheHandlerYouWant file_handler = TheHandlerYouWant(...) file_handler.setLevel(logging.WARNING) app.logger.addHandler(file_handler)
原创粉丝点击