]> git.ipfire.org Git - thirdparty/python-fints.git/commitdiff
Add configuration and code to opt-in to reduced logging. (#117)
authorHenryk Plötz <henryk@ploetzli.ch>
Thu, 28 Mar 2024 08:40:38 +0000 (09:40 +0100)
committerGitHub <noreply@github.com>
Thu, 28 Mar 2024 08:40:38 +0000 (09:40 +0100)
Currently this strips all the boilerplate prefix and suffix from the packet dump in FinTSHTTPSConnection.send(), only showing the net payload.

fints/connection.py
fints/utils.py

index d0530b984ef07cd6327f44785f7f6bf2fd351045..c01bb0355abf0f8fbcc6076d1d9f4d1e801f1284 100644 (file)
@@ -3,14 +3,27 @@ import io
 import logging
 
 import requests
-from fints.utils import Password
+from fints.utils import Password, log_configuration
 
 from .exceptions import *
 from .message import FinTSInstituteMessage, FinTSMessage
+from .types import SegmentSequence
 
 logger = logging.getLogger(__name__)
 
 
+def reduce_message_for_log(msg):
+    log_msg = msg
+    if log_configuration.reduced:
+        # Try to find a single inner message
+        if len(log_msg.segments) == 4 and log_msg.segments[2].header.type == 'HNVSD':
+            log_msg = log_msg.segments[2]
+            if len(log_msg.data.segments) > 2 and log_msg.data.segments[0].header.type == "HNSHK" and \
+                    log_msg.data.segments[-1].header.type == "HNSHA":
+                log_msg = SegmentSequence(segments=log_msg.data.segments[1:-1])
+    return log_msg
+
+
 class FinTSHTTPSConnection:
     def __init__(self, url):
         self.url = url
@@ -19,8 +32,9 @@ class FinTSHTTPSConnection:
     def send(self, msg: FinTSMessage):
         log_out = io.StringIO()
         with Password.protect():
-            msg.print_nested(stream=log_out, prefix="\t")
-            logger.debug("Sending >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n{}\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n".format(log_out.getvalue()))
+            log_msg = reduce_message_for_log(msg)
+            log_msg.print_nested(stream=log_out, prefix="\t")
+            logger.debug("Sending {}>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n{}\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n".format("(abbrv.)" if log_configuration.reduced else "", log_out.getvalue()))
             log_out.truncate(0)
 
         r = self.session.post(
@@ -37,6 +51,7 @@ class FinTSHTTPSConnection:
         retval = FinTSInstituteMessage(segments=response)
 
         with Password.protect():
-            retval.print_nested(stream=log_out, prefix="\t")
-            logger.debug("Received <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n{}\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n".format(log_out.getvalue()))
+            log_msg = reduce_message_for_log(retval)
+            log_msg.print_nested(stream=log_out, prefix="\t")
+            logger.debug("Received {}<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n{}\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n".format("(abbrv.)" if log_configuration.reduced else "", log_out.getvalue()))
         return retval
index 6d63fe26aa76bcf0487a62153cbe5f988035145d..fa408d6191e01563b713564d909e9cdfc1ffa5a7 100644 (file)
@@ -2,6 +2,7 @@ import base64
 import inspect
 import json
 import re
+import threading
 import zlib
 from contextlib import contextmanager
 from datetime import datetime
@@ -308,3 +309,30 @@ def minimal_interactive_cli_bootstrap(client):
                           p=mm))
             choice = input("Choice: ").strip()
             client.set_tan_medium(m[1][int(choice)])
+
+
+class LogConfiguration(threading.local):
+    """Thread-local configuration object to guide log output.
+
+    reduced: Reduce verbosity of logging output by suppressing the encrypting/signature elements and outputting the payload only.
+    """
+    def __init__(self, reduced=False):
+        super().__init__()
+        self.reduced = reduced
+
+    @staticmethod
+    def set(reduced=False):
+        """Permanently change the log configuration for this thread."""
+        log_configuration.reduced = reduced
+
+    @staticmethod
+    @contextmanager
+    def changed(reduced=False):
+        """Temporarily change the log configuration for this thread."""
+        old_reduced = log_configuration.reduced
+        log_configuration.set(reduced=reduced)
+        yield
+        log_configuration.set(reduced=old_reduced)
+
+
+log_configuration = LogConfiguration()