From 800f6901a265cd815ab199f8ebf9c07629ed5daa Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Sun, 25 Oct 2020 00:49:30 +0200 Subject: [PATCH 1/3] Add INFO logs for aiohttp transport --- gql/transport/aiohttp.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/gql/transport/aiohttp.py b/gql/transport/aiohttp.py index cd5b44a0..857edfab 100644 --- a/gql/transport/aiohttp.py +++ b/gql/transport/aiohttp.py @@ -183,6 +183,9 @@ async def execute( if variable_values: payload["variables"] = variable_values + if log.isEnabledFor(logging.INFO): + log.info(">>> %s", json.dumps(payload)) + post_args = {"json": payload} # Pass post_args to aiohttp post method @@ -195,6 +198,10 @@ async def execute( async with self.session.post(self.url, ssl=self.ssl, **post_args) as resp: try: result = await resp.json() + + if log.isEnabledFor(logging.INFO): + result_text = await resp.text() + log.info("<<< %s", result_text) except Exception: # We raise a TransportServerError if the status code is 400 or higher # We raise a TransportProtocolError in the other cases From 1bb74c25b9eee5008d72995b76ac4e776050630b Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Sun, 25 Oct 2020 01:14:23 +0200 Subject: [PATCH 2/3] DOC add Logging documentation --- docs/advanced/index.rst | 1 + docs/advanced/logging.rst | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 docs/advanced/logging.rst diff --git a/docs/advanced/index.rst b/docs/advanced/index.rst index 3767112b..637a8ea4 100644 --- a/docs/advanced/index.rst +++ b/docs/advanced/index.rst @@ -5,5 +5,6 @@ Advanced :maxdepth: 2 async_advanced_usage + logging local_schema dsl_module diff --git a/docs/advanced/logging.rst b/docs/advanced/logging.rst new file mode 100644 index 00000000..573a83be --- /dev/null +++ b/docs/advanced/logging.rst @@ -0,0 +1,21 @@ +Logging +======= + +GQL use the python `logging`_ module. + +In order to debug a problem, you can enable logging to see the messages exchanged between the client and the server. +To do that, set the loglevel at **INFO** at the beginning of your code: + +.. code-block:: python + + import logging + logging.basicConfig(level=logging.INFO) + +For even more logs, you can set the loglevel at **DEBUG**: + +.. code-block:: python + + import logging + logging.basicConfig(level=logging.DEBUG) + +.. _logging: https://docs.python.org/3/howto/logging.html From 2aa6d9bf09553e4a1f51d929385034b97755ccbc Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Thu, 29 Oct 2020 17:28:32 +0100 Subject: [PATCH 3/3] Add logging to requests too --- gql/transport/requests.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gql/transport/requests.py b/gql/transport/requests.py index 823d0bc6..5eb2a36c 100644 --- a/gql/transport/requests.py +++ b/gql/transport/requests.py @@ -1,3 +1,5 @@ +import json +import logging from typing import Any, Dict, Optional, Union import requests @@ -15,6 +17,8 @@ TransportServerError, ) +log = logging.getLogger(__name__) + class RequestsHTTPTransport(Transport): """:ref:`Sync Transport ` used to execute GraphQL queries @@ -135,6 +139,10 @@ def execute( # type: ignore data_key: payload, } + # Log the payload + if log.isEnabledFor(logging.INFO): + log.info(">>> %s", json.dumps(payload)) + # Pass kwargs to requests post method post_args.update(self.kwargs) @@ -144,6 +152,9 @@ def execute( # type: ignore ) try: result = response.json() + + if log.isEnabledFor(logging.INFO): + log.info("<<< %s", response.text) except Exception: # We raise a TransportServerError if the status code is 400 or higher # We raise a TransportProtocolError in the other cases