From: Daniele Varrazzo Date: Fri, 9 May 2025 00:48:25 +0000 (+0200) Subject: feat: restore connection attempts logging X-Git-Tag: 3.2.8~8^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d2d7c3f7f8230b94bca208913c6dfe501a5cda18;p=thirdparty%2Fpsycopg.git feat: restore connection attempts logging It was deleted in #1076, with the introduction of the multi-message exception but it is still useful, looking at #888. --- diff --git a/psycopg/psycopg/connection.py b/psycopg/psycopg/connection.py index 7cc1eee46..ad2e3c1af 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -96,20 +96,22 @@ class Connection(BaseConnection[Row]): attempts = conninfo_attempts(params) connection_errors: list[tuple[e.Error, str]] = [] for attempt in attempts: + tlog = (attempt.get("host"), attempt.get("port"), attempt.get("hostaddr")) + logger.debug("connection attempt: host=%r port=%r hostaddr=%r", *tlog) try: conninfo = make_conninfo("", **attempt) gen = cls._connect_gen(conninfo, timeout=timeout) rv = waiting.wait_conn(gen, interval=_WAIT_INTERVAL) except e.Error as ex: - attempt_details = "host: {}, port: {}, hostaddr: {}".format( - repr(attempt.get("host")), - repr(attempt.get("port")), - repr(attempt.get("hostaddr")), + logger.debug( + "connection failed: host=%r port=%r hostaddr=%r: %s", *tlog, str(ex) ) + attempt_details = "host: %r, port: %r, hostaddr: %r" % tlog connection_errors.append((ex, attempt_details)) except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) else: + logger.debug("connection succeeded: host=%r port=%r hostaddr=%r", *tlog) break if not rv: diff --git a/psycopg/psycopg/connection_async.py b/psycopg/psycopg/connection_async.py index 497342a47..db4135553 100644 --- a/psycopg/psycopg/connection_async.py +++ b/psycopg/psycopg/connection_async.py @@ -114,20 +114,24 @@ class AsyncConnection(BaseConnection[Row]): attempts = await conninfo_attempts_async(params) connection_errors: list[tuple[e.Error, str]] = [] for attempt in attempts: + tlog = (attempt.get("host"), attempt.get("port"), attempt.get("hostaddr")) + logger.debug("connection attempt: host=%r port=%r hostaddr=%r", *tlog) try: conninfo = make_conninfo("", **attempt) gen = cls._connect_gen(conninfo, timeout=timeout) rv = await waiting.wait_conn_async(gen, interval=_WAIT_INTERVAL) except e.Error as ex: - attempt_details = "host: {}, port: {}, hostaddr: {}".format( - repr(attempt.get("host")), - repr(attempt.get("port")), - repr(attempt.get("hostaddr")), + logger.debug( + "connection failed: host=%r port=%r hostaddr=%r: %s", + *tlog, + str(ex), ) + attempt_details = "host: %r, port: %r, hostaddr: %r" % tlog connection_errors.append((ex, attempt_details)) except e._NO_TRACEBACK as ex: raise ex.with_traceback(None) else: + logger.debug("connection succeeded: host=%r port=%r hostaddr=%r", *tlog) break if not rv: