From: Daniele Varrazzo Date: Fri, 9 May 2025 00:48:25 +0000 (+0200) Subject: feat: restore connection attempts logging X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=60a1cd5622c8e320fa1d5e9a302fa115d2ccc45d;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 f2b3a748e..75146fda0 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -97,20 +97,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 19334c280..62940a6da 100644 --- a/psycopg/psycopg/connection_async.py +++ b/psycopg/psycopg/connection_async.py @@ -113,20 +113,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: