Skip to content

Commit

Permalink
Add configuration and code to opt-in to reduced logging. (#117)
Browse files Browse the repository at this point in the history
Currently this strips all the boilerplate prefix and suffix from the packet dump in FinTSHTTPSConnection.send(), only showing the net payload.
  • Loading branch information
henryk committed Mar 28, 2024
1 parent 106f5e8 commit 4940f12
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
25 changes: 20 additions & 5 deletions fints/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@
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
Expand All @@ -19,8 +32,9 @@ def __init__(self, url):
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(
Expand All @@ -37,6 +51,7 @@ def send(self, msg: FinTSMessage):
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
28 changes: 28 additions & 0 deletions fints/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import inspect
import json
import re
import threading
import zlib
from contextlib import contextmanager
from datetime import datetime
Expand Down Expand Up @@ -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()

0 comments on commit 4940f12

Please sign in to comment.