【发布时间】:2021-06-11 11:45:24
【问题描述】:
目标是完成一个 Flask 教程,其中使用 logging.handler.SMTPHandler 从 Python 将日志发送到 SMTP 调试服务器。
Python服务器在windows cli下运行,以管理员身份打开一个新的windows CLI并运行:python -m smtpd -n -c DebuggingServer localhost:1025
要测试它,下面的代码应该可以工作:
import logging
import sys
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
smtpHandler = logging.handlers.SMTPHandler(
mailhost = ("localhost",8025),
fromaddr = "alerts@localhost",
toaddrs = "geo555@localhost",
subject = "alert!"
)
smtpHandler.setLevel(logging.DEBUG)
logger.debug("here is the test logging for u.")
目前已尝试,调试服务器中没有出现任何消息:
- https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vii-error-handling 不向调试服务器发送任何输出。
- How can I send an email using python logging's SMTPHandler and SSL 没有明确使用调试服务器,它没有工作。
什么可以是一个非常简单的例子可以做到这一点,否则使用调试文件也可以。 干杯。
【问题讨论】: