changeset 3881:2e4a0f6050bd

component AP gateway: better verbose logging: gather data to show according to verbosity and log it all at once with a small `<<<` or `>>>` indicating the direction. With a verbosity of 3, headers are also shown.
author Goffi <goffi@goffi.org>
date Wed, 31 Aug 2022 17:07:03 +0200
parents 03761f8ba8bb
children 1bd44367337d
files sat/plugins/plugin_comp_ap_gateway/__init__.py sat/plugins/plugin_comp_ap_gateway/http_server.py
diffstat 2 files changed, 30 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_comp_ap_gateway/__init__.py	Wed Aug 31 17:07:01 2022 +0200
+++ b/sat/plugins/plugin_comp_ap_gateway/__init__.py	Wed Aug 31 17:07:03 2022 +0200
@@ -1166,9 +1166,10 @@
         if self.verbose:
             __, actor_args = self.parseAPURL(actor_id)
             actor_account = actor_args[0]
-            log.info(
-                f"==> {actor_account} is signing and posting to {url}:\n{pformat(doc)}"
-            )
+            to_log = [
+                "",
+                f">>> {actor_account} is signing and posting to {url}:\n{pformat(doc)}"
+            ]
 
         p_url = parse.urlparse(url)
         body = json.dumps(doc).encode()
@@ -1183,9 +1184,13 @@
         }
         headers, __ = self.getSignatureData(self.getKeyId(actor_id), headers)
 
-        headers["Content-Type"] = (
-            'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'
-        )
+        if self.verbose:
+            if self.verbose>=3:
+                h_to_log = "\n".join(f"    {k}: {v}" for k,v in headers.items())
+                to_log.append(f"  headers:\n{h_to_log}")
+            to_log.append("---")
+            log.info("\n".join(to_log))
+
         resp = await treq.post(
             url,
             body,
--- a/sat/plugins/plugin_comp_ap_gateway/http_server.py	Wed Aug 31 17:07:01 2022 +0200
+++ b/sat/plugins/plugin_comp_ap_gateway/http_server.py	Wed Aug 31 17:07:03 2022 +0200
@@ -821,18 +821,25 @@
         signing_actor: Optional[str] = None
     ) -> None:
         if self.apg.verbose:
+            from pprint import pformat
+            to_log = [
+                "",
+                f"<<< got {request.method.decode()} request - {request.uri.decode()}"
+            ]
             try:
                 data = json.load(request.content)
-            except (json.JSONDecodeError, ValueError) as e:
+            except (json.JSONDecodeError, ValueError):
                 pass
             else:
-                from pprint import pformat
-                log.info(
-                    f"==> got {request.method.decode()} request:\n{pformat(data)}\n---"
-                )
-
+                to_log.append(pformat(data))
             finally:
                 request.content.seek(0)
+            if self.apg.verbose>=3:
+                headers = "\n".join(
+                    f"    {k.decode()}: {v.decode()}"
+                    for k,v in request.getAllHeaders().items()
+                )
+                to_log.append(f"  headers:\n{headers}")
 
         path = request.path.decode()
         ap_url = parse.urljoin(
@@ -871,20 +878,13 @@
         if ret_data is not None:
             request.setHeader("content-type", CONTENT_TYPE_AP)
             request.write(json.dumps(ret_data).encode())
+        if self.apg.verbose:
+            to_log.append(f"--- RET (code: {request.code})---")
             if self.apg.verbose>=2:
-                from pprint import pformat
-                to_log = [f"==> ret (code: {request.code}):\n{pformat(ret_data)}"]
-                log.info(
-
-                )
-                if self.apg.verbose>=3:
-                    headers = "\n".join(
-                        f"    {k.decode()}: {v.decode()}"
-                        for k,v in request.getAllHeaders().items()
-                    )
-                    to_log.append("  headers:\n{headers}")
-                to_log.append("---")
-                log.info("\n".join(to_log))
+                if ret_data is not None:
+                    to_log.append(f"{pformat(ret_data)}")
+                    to_log.append("---")
+            log.info("\n".join(to_log))
         request.finish()
 
     async def APPostRequest(self, request: "HTTPRequest"):