简体   繁体   English

python logging.handlers.SMTPHandler 的问题,“凭据”未被识别为 SMTPHandler 的属性

[英]Problem with python logging.handlers.SMTPHandler, 'credentials' not recognized as attribute of SMTPHandler

I'm trying to set up email logging of critical errors in my python application.我正在尝试在我的 python 应用程序中设置严重错误的 email 日志记录。 I keep running into an error trying to initialize the SMTPHandler:我在尝试初始化 SMTPHandler 时一直遇到错误:

AttributeError: 'SMTPHandler' object has no attribute 'credentials' AttributeError: 'SMTPHandler' object 没有属性 'credentials'

I'm using Python 3.10.我正在使用 Python 3.10。 I carved out a component of the program where I'm getting the error.我在出现错误的地方创建了一个程序组件。

import logging
from logging.handlers import SMTPHandler

mail_handler = SMTPHandler(
    mailhost='my.hosting.com',
    fromaddr='admin@myapp.com',
    toaddrs=['admin@myapp.com'],
    subject='Application Error', 
    credentials=('admin@myapp.com', 'mypassword'),
    secure=()
)
print(mail_handler.mailhost)
print(mail_handler.fromaddr)
print(mail_handler.toaddrs)
print(mail_handler.subject)
print(mail_handler.secure)
print(mail_handler.timeout)
print(mail_handler.credentials)
mail_handler.setLevel(logging.ERROR)
mail_handler.setFormatter(logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s'))

The print statements and traceback I'm getting is:我得到的打印语句和回溯是:

my.hosting.com
admin@myapp.com
['admin@myapp.com']
Application Error
()
5.0
Traceback (most recent call last):
  File "C:\Users\user\Documents\myapp\test.py", line 31, in <module>
    print(mail_handler.credentials)
AttributeError: 'SMTPHandler' object has no attribute 'credentials'

When I check the init statement for SMTPHandler using the following snippet to make sure I'm not accessing a very old version (I think credentials was added in 2.6):当我使用以下代码片段检查 SMTPHandler 的 init 语句以确保我没有访问非常旧的版本时(我认为凭据是在 2.6 中添加的):

import inspect

signature = inspect.signature(SMTPHandler.__init__).parameters
for name, parameter in signature.items():
    print(name, parameter.default, parameter.annotation, parameter.kind)`

I get:我得到:

self <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
mailhost <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
fromaddr <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
toaddrs <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
subject <class 'inspect._empty'> <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
credentials None <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
secure None <class 'inspect._empty'> POSITIONAL_OR_KEYWORD
timeout 5.0 <class 'inspect._empty'> POSITIONAL_OR_KEYWORD

So 'credentials' is in the initialization statement.所以'credentials'在初始化语句中。

Anyone see something stupid in my code or run into this problem?有人在我的代码中看到了一些愚蠢的东西或遇到了这个问题吗?

Thanks so much!非常感谢!

You have the full source code for all of the standard modules on your computer.您在计算机上拥有所有标准模块的完整源代码。 I just took a quick look, and although the SMTPHandler accepts a credentials argument, it stores that argument in self.username and self.password .我只是快速浏览了一下,虽然SMTPHandler接受一个credentials参数,但它将该参数存储在self.usernameself.password中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM