Skip to content

Commit 48cb1cf

Browse files
committed
added log_responses parameter and IBIND_LOG_RESPONSES env var (False by default) that can be used to log responses received from the broker to a file
1 parent 7ef09e5 commit 48cb1cf

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

ibind/base/rest_client.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def __init__(
8282
max_retries: int = 3,
8383
use_session: bool = var.IBIND_USE_SESSION,
8484
auto_register_shutdown: bool = var.IBIND_AUTO_REGISTER_SHUTDOWN,
85+
log_responses: bool = var.IBIND_LOG_RESPONSES,
8586
) -> None:
8687
"""
8788
Parameters:
@@ -106,6 +107,7 @@ def __init__(
106107

107108
self._timeout = timeout
108109
self._max_retries = max_retries
110+
self._log_responses = log_responses
109111

110112
self._make_logger()
111113

@@ -237,24 +239,24 @@ def _request(
237239
try:
238240
response = request_function(method, url, verify=self.cacert, headers=headers, timeout=self._timeout, **kwargs)
239241
result = Result(request={'url': url, **kwargs})
240-
return self._process_response(response, result)
242+
result = self._process_response(response, result)
243+
if self._log_responses:
244+
self.logger.info(result)
245+
return result
241246

242247
except ReadTimeout as e:
243248
if attempt >= self._max_retries:
244249
raise TimeoutError(f'{self}: Reached max retries ({self._max_retries}) for {method} {url} {kwargs}') from e
245-
246-
self.logger.info(f'{self}: Timeout for {method} {url} {kwargs}, retrying attempt {attempt + 1}/{self._max_retries}')
247-
_LOGGER.info(f'{self}: Timeout for {method} {url} {kwargs}, retrying attempt {attempt + 1}/{self._max_retries}')
250+
msg = f'{self}: Timeout for {method} {url} {kwargs}, retrying attempt {attempt + 1}/{self._max_retries}'
251+
self.logger.info(msg)
252+
_LOGGER.info(msg)
248253

249254
continue # Continue to the next iteration for a retry
250255

251256
except (ConnectionError, ChunkedEncodingError) as e:
252-
self.logger.warning(
253-
f'{self}: Connection error detected, resetting session and retrying attempt {attempt + 1}/{self._max_retries} :: {str(e)}'
254-
)
255-
_LOGGER.warning(
256-
f'{self}: Connection error detected, resetting session and retrying attempt {attempt + 1}/{self._max_retries} :: {str(e)}'
257-
)
257+
msg = f'{self}: Connection error detected, resetting session and retrying attempt {attempt + 1}/{self._max_retries} :: {str(e)}'
258+
self.logger.warning(msg)
259+
_LOGGER.warning(msg)
258260
self.close()
259261
if self.use_session:
260262
self.make_session() # Recreate session automatically

ibind/client/ibkr_client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def __init__(
5151
max_retries: int = 3,
5252
use_session: bool = var.IBIND_USE_SESSION,
5353
auto_register_shutdown: bool = var.IBIND_AUTO_REGISTER_SHUTDOWN,
54+
log_responses: bool = var.IBIND_LOG_RESPONSES,
5455
use_oauth: bool = var.IBIND_USE_OAUTH,
5556
oauth_config: 'OAuthConfig' = None,
5657
) -> None:
@@ -97,6 +98,7 @@ def __init__(
9798
max_retries=max_retries,
9899
use_session=use_session,
99100
auto_register_shutdown=auto_register_shutdown,
101+
log_responses=log_responses,
100102
)
101103

102104
self.logger.info('#################')

ibind/var.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def to_bool(value):
3939
IBIND_AUTO_REGISTER_SHUTDOWN = to_bool(os.environ.get('IBIND_AUTO_REGISTER_SHUTDOWN', True))
4040
""" Whether to automatically register the shutdown handler. """
4141

42+
IBIND_LOG_RESPONSES = to_bool(os.environ.get('IBIND_LOG_RESPONSES', False))
43+
""" Whether to log responses coming from the broker. """
44+
4245
##### LOGS #####
4346

4447
LOG_TO_CONSOLE = to_bool(os.environ.get('IBIND_LOG_TO_CONSOLE', True))

0 commit comments

Comments
 (0)