Skip to content

Commit

Permalink
feat(logging): Adds info and debug logging (#47)
Browse files Browse the repository at this point in the history
* feat(logging): Adds info and debug logging

Signed-off-by: GustaafL <guus@seita.nl>

* chore: run precommit

Signed-off-by: GustaafL <guus@seita.nl>

* feat(logging): changed logging types for review

Signed-off-by: GustaafL <guus@seita.nl>

* chore(logging): run pre-commit

Signed-off-by: GustaafL <guus@seita.nl>

* feat(logs): logging closer to request

Signed-off-by: GustaafL <guus@seita.nl>

* feat(logging): using consistant format and footer

Signed-off-by: GustaafL <guus@seita.nl>

* feat(logging): fix double function call issue from merge

Signed-off-by: GustaafL <guus@seita.nl>

---------

Signed-off-by: GustaafL <guus@seita.nl>
  • Loading branch information
GustaafL authored Jun 27, 2023
1 parent cda8b78 commit 55088c5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
28 changes: 26 additions & 2 deletions src/flexmeasures_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ async def request(
if response.status < 300:
break
except asyncio.TimeoutError:
message = f"Client request timeout occurred while connecting to the API. Retrying in {self.polling_interval} seconds..." # noqa: E501
logging.info(message)
message = f"Client request timeout occurred while connecting to the API. Polling step: {polling_step}. Retrying in {self.polling_interval} seconds..." # noqa: E501
logging.debug(message)
polling_step += 1
await asyncio.sleep(self.polling_interval)
except (ClientError, socket.gaierror) as exception:
Expand All @@ -151,6 +151,19 @@ async def request_once(
polling_step: int = 0,
reauth_once: bool = True,
) -> tuple[ClientResponse, int, bool]:
url_msg = f"url: {url}"
json_msg = f"payload: {json}"
params_msg = f"params: {params}"
method_msg = f"method: {method}"
headers_msg = f"headers: {headers}"
logging.debug("===== Request =====")
logging.debug(url_msg)
logging.debug(json_msg)
logging.debug(params_msg)
logging.debug(method_msg)
logging.debug(headers_msg)
logging.debug("=" * 14)

"""Sends a single request to FlexMeasures and checks the response"""
response = await self.session.request(
method=method,
Expand All @@ -160,6 +173,17 @@ async def request_once(
json=json,
ssl=self.ssl,
)
payload = await response.json()
status_msg = f"status: {response.status}"
response_payload_msg = f"payload: {payload}"
headers_msg = f"headers: {response.headers}"

logging.debug("===== Response =====")
logging.debug(status_msg)
logging.debug(response_payload_msg)
logging.debug(headers_msg)
logging.debug("=" * 14)

polling_step, reauth_once = await check_response(
self, response, polling_step, reauth_once
)
Expand Down
15 changes: 14 additions & 1 deletion src/flexmeasures_client/response_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ async def check_response(
if status < 300:
pass
elif status == 401 and reauth_once:
message = f"""Authentication failed with"
status: {status}
headers: {headers}
payload: {payload}.
Re-authenticating!
"""
logging.debug(message)
await self.get_access_token()
reauth_once = False
elif status == 503 and "Retry-After" in headers:
Expand All @@ -38,13 +45,19 @@ async def check_response(
):
# can be removed in a later version GH issue #645 of the FlexMeasures repo
message = f"Server indicated to try again later. Retrying in {self.polling_interval} seconds..." # noqa: E501
logging.info(message)
logging.debug(message)
polling_step += 1
await asyncio.sleep(self.polling_interval)
elif payload.get("errors"):
# try to raise any error messages from the response
raise ValueError(" ,".join(payload.get("errors")))
else:
message = f"""
status: {status}
headers: {headers}
payload: {payload}.
"""
logging.error(message)
# otherwise, raise if the status does not indicate okay
response.raise_for_status()
return polling_step, reauth_once
Expand Down

0 comments on commit 55088c5

Please sign in to comment.