Pythonlogging SMTPHandler - 处理离线SMTP服务器
我已经为我的新 python 脚本设置了日志记录模块。我有两个处理程序,一个将内容发送到文件,另一个用于电子邮件警报。 SMTPHandler 设置为发送 ERROR 级别或更高级别的任何内容。
除非 SMTP 连接失败,否则一切正常。如果 SMTP 服务器没有响应或身份验证失败(需要 SMTP 身份验证),则整个脚本就会终止。
我对 python 相当陌生,所以我试图弄清楚如何捕获 SMTPHandler 引发的异常,以便通过电子邮件发送日志消息的任何问题都不会导致我的整个脚本崩溃。由于我还将错误写入日志文件,因此如果 SMTP 警报失败,我只想继续前进,而不是停止任何操作。
如果我需要一个“try:”语句,它会绕过logging.handlers.SMTPHandler设置,还是绕过对my_logger.error()的单独调用?
I have setup the logging module for my new python script. I have two handlers, one sending stuff to a file, and one for email alerts. The SMTPHandler is setup to mail anything at the ERROR level or above.
Everything works great, unless the SMTP connection fails. If the SMTP server does not respond or authentication fails (it requires SMTP auth), then the whole script dies.
I am fairly new to python, so I am trying to figure out how to capture the exception that the SMTPHandler is raising so that any problems sending the log message via email won't bring down my entire script. Since I am also writing errors to a log file, if the SMTP alert fails, I just want to keep going, not halt anything.
If I need a "try:" statement, would it go around the logging.handlers.SMTPHandler setup, or around the individual calls to my_logger.error()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
日志记录期间发生的异常不应停止您的脚本,尽管它们可能会导致回溯打印到 sys.stderr 。为了防止出现此打印输出,请执行以下操作:
这不是默认设置(因为在开发中您通常想了解故障),但在生产中,不应设置
raiseExceptions
。您应该发现
SMTPHandler
将在下次记录错误(或更高错误)时尝试重新连接。logging
确实通过SystemExit
和KeyboardInterrupt
异常,但应处理所有其他异常,以免导致终止使用日志记录的应用程序。如果您发现情况并非如此,请发布正在发生并导致脚本终止的异常的具体详细信息,以及有关您的 Python/操作系统版本的信息。请记住,如果网络超时导致处理程序阻塞(例如,如果 DNS 查找需要很长时间,或者 SMTP 连接需要很长时间),则脚本可能会挂起。Exceptions which occur during logging should not stop your script, though they may cause a traceback to be printed to
sys.stderr
. In order to prevent this printout, do the following:This is not the default (because in development you typically want to know about failures) but in production,
raiseExceptions
should not be set.You should find that the
SMTPHandler
will attempt a re-connection the next time an ERROR (or higher) is logged.logging
does pass throughSystemExit
andKeyboardInterrupt
exceptions, but all others should be handled so that they do not cause termination of an application which uses logging. If you find that this is not the case, please post specific details of the exceptions which are getting through and causing your script to terminate, and about your version of Python/operating system. Bear in mind that the script may appear to hang if there is a network timeout which is causing the handler to block (e.g. if a DNS lookup takes a long time, or if the SMTP connection takes a long time).你可能需要两者都做。为了解决这个问题,我建议安装一个本地邮件服务器并使用它。这样,您可以在脚本运行时将其关闭并记下错误消息。
为了保持代码的可维护性,您应该以可以在单个位置处理异常的方式扩展 SMTPHandler(而不是使用 try-except 包装每个记录器调用)。
You probably need to do both. To figure this out, I suggest to install a local mail server and use that. This way, you can shut it down while your script runs and note down the error message.
To keep the code maintainable, you should extends SMTPHandler in such a way that you can handle the exceptions in a single place (instead of wrapping every logger call with try-except).