Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/advanced/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ Advanced
:maxdepth: 2

async_advanced_usage
logging
local_schema
dsl_module
21 changes: 21 additions & 0 deletions docs/advanced/logging.rst
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions gql/transport/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions gql/transport/requests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json
import logging
from typing import Any, Dict, Optional, Union

import requests
Expand All @@ -15,6 +17,8 @@
TransportServerError,
)

log = logging.getLogger(__name__)


class RequestsHTTPTransport(Transport):
""":ref:`Sync Transport <sync_transports>` used to execute GraphQL queries
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down