Skip to content

Commit

Permalink
Merge pull request #67 from datastax/feature/#23-logging
Browse files Browse the repository at this point in the history
Add optional debugging logging for each request/response
  • Loading branch information
erichare committed Oct 30, 2023
2 parents 5cfe810 + 46dabd1 commit 92ee455
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions astrapy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from astrapy.defaults import DEFAULT_TIMEOUT

logger = logging.getLogger(__name__)
# logger.setLevel(logging.DEBUG) # Apply if wishing to debug requests


class http_methods:
Expand All @@ -19,6 +20,19 @@ class http_methods:
package_name = __name__.split(".")[0]


def log_request_response(r, json_data):
logger.debug(f"Request URL: {r.url}")
logger.debug(f"Request method: {r.request.method}")
logger.debug(f"Request headers: {r.request.headers}")

if json_data:
logger.debug(f"Request payload: {json_data}")

logger.debug(f"Response status code: {r.status_code}")
logger.debug(f"Response headers: {r.headers}")
logger.debug(f"Response content: {r.text}")


def make_request(
base_url,
auth_header,
Expand All @@ -37,6 +51,9 @@ def make_request(
headers={auth_header: token, "User-Agent": f"{package_name}/{__version__}"},
)

if logger.isEnabledFor(logging.DEBUG):
log_request_response(r, json_data)

return r


Expand Down

0 comments on commit 92ee455

Please sign in to comment.