]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
feat: restore connection attempts logging
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 9 May 2025 00:48:25 +0000 (02:48 +0200)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sat, 10 May 2025 02:44:06 +0000 (04:44 +0200)
It was deleted in #1076, with the introduction of the multi-message
exception but it is still useful, looking at #888.

psycopg/psycopg/connection.py
psycopg/psycopg/connection_async.py

index f2b3a748ed47d52bebe9c18346c0717c744ed876..75146fda01f3030b41ee9ec9d47546b07563c4d8 100644 (file)
@@ -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:
index 19334c28013807a15f0e07316ab83b3f148736c8..62940a6dac05283ec7a272320337049f8c83bae1 100644 (file)
@@ -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: