]> 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 13:14:03 +0000 (15:14 +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 7cc1eee46ef67763ef132e0624be19914e3670bc..ad2e3c1af3cd5bea847882286e22ec7004d755e5 100644 (file)
@@ -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:
index 497342a474f826d41c6d5eb0da2e87a2b9c502d1..db4135553520da4dd64e5102f4e97df618ce5dfe 100644 (file)
@@ -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: