I am currently trying to log using the SMTPHandler class from Python’s logging module. The logger is instantiated just fine, but fails when calling _socket.getaddrinfo
even though it is being passed parameters which seem to work elsewhere. Below is the error message I get:
--- Logging error ---
Traceback (most recent call last):
File "C:UsersUserAppDataLocalProgramsPythonPython311Liblogginghandlers.py", line 1075, in emit
smtp = smtplib.SMTP(self.mailhost, port, timeout=self.timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsmtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsmtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsmtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsocket.py", line 827, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:UsersUserAppDataLocalProgramsPythonPython311Libsocket.py", line 962, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
socket.gaierror: [Errno 11001] getaddrinfo failed
Call stack:
File "<string>", line 1, in <module>
File "C:UsersUserAppDataLocalProgramsPythonPython311Libidlelibrun.py", line 165, in main
ret = method(*args, **kwargs)
File "C:UsersUserAppDataLocalProgramsPythonPython311Libidlelibrun.py", line 579, in runcode
exec(code, self.locals)
File "C:UsersUserDesktoptest_logtest_log.py", line 34, in <module>
main()
File "C:UsersUserDesktoptest_logtest_log.py", line 28, in main
log.critical('Testing email logging')
Message: 'Testing email logging'
Arguments: ()
I’m configuring the email handler and attempting to log as below.
Handler Config:
"emailhand": {
"class": "logging.handlers.SMTPHandler",
"level": "CRITICAL",
"formatter": "a_formatter",
"mailhost": "('send.smtp.com', 25)",
"credentials": "('[email protected]', 'password')",
"fromaddr": "[email protected]",
"toaddrs": "[email protected]",
"subject": "Test Log",
"secure": "()",
"timeout": 10.0
}
Python Module:
import os
import json
import logging
import logging.config
import logging.handlers
def init_logging():
logfile = r'C:UsersUserDesktoptest_logtest_log.log'
with open(r'C:UsersUserDesktoptest_logfile-datalogging.json', 'r') as f:
LOGGING_CONFIG = json.load(f)
logging.config.dictConfig(LOGGING_CONFIG)
def main():
init_logging()
log = logging.getLogger('test_log')
log.info('Some info from the main function')
log.critical('Testing email logging')
if __name__ == "__main__":
main()
Currently I’m using TLS when connecting to the email server, but I’ve also tried with SSL and port 465 and this hasn’t worked either. I’ve been able to successfully send emails using the smtp library with both TLS and SSL outside of this module by using smtplib.SMTP. I’ve attempted to alter the timeout value (anywhere from 1 to 10000) and this seems to have made no difference. I’ve also attempted to alter the emit function to use SMTP_SSL instead of SMTP and this didn’t work. I’ve run socket.getaddrinfo with the same host and port as above and received the following (I’ve replaced the IP numbers with ‘#’):
[(<AddressFamily.AF_INET: 2>, 0, 0, '', ('###.##.###.##', 25)), (<AddressFamily.AF_INET: 2>, 0, 0, '', ('###.##.###.##', 25))]
As far as I can tell the above means that socket is working fine. What could be causing this issue?
RealSwellThanks is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.