Skip to content

Commit 9d79b11

Browse files
authored
Feature aiohttp and requests transports logging (#157)
1 parent d9788dd commit 9d79b11

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

docs/advanced/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ Advanced
55
:maxdepth: 2
66

77
async_advanced_usage
8+
logging
89
local_schema
910
dsl_module

docs/advanced/logging.rst

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Logging
2+
=======
3+
4+
GQL use the python `logging`_ module.
5+
6+
In order to debug a problem, you can enable logging to see the messages exchanged between the client and the server.
7+
To do that, set the loglevel at **INFO** at the beginning of your code:
8+
9+
.. code-block:: python
10+
11+
import logging
12+
logging.basicConfig(level=logging.INFO)
13+
14+
For even more logs, you can set the loglevel at **DEBUG**:
15+
16+
.. code-block:: python
17+
18+
import logging
19+
logging.basicConfig(level=logging.DEBUG)
20+
21+
.. _logging: https://docs.python.org/3/howto/logging.html

gql/transport/aiohttp.py

+7
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ async def execute(
183183
if variable_values:
184184
payload["variables"] = variable_values
185185

186+
if log.isEnabledFor(logging.INFO):
187+
log.info(">>> %s", json.dumps(payload))
188+
186189
post_args = {"json": payload}
187190

188191
# Pass post_args to aiohttp post method
@@ -195,6 +198,10 @@ async def execute(
195198
async with self.session.post(self.url, ssl=self.ssl, **post_args) as resp:
196199
try:
197200
result = await resp.json()
201+
202+
if log.isEnabledFor(logging.INFO):
203+
result_text = await resp.text()
204+
log.info("<<< %s", result_text)
198205
except Exception:
199206
# We raise a TransportServerError if the status code is 400 or higher
200207
# We raise a TransportProtocolError in the other cases

gql/transport/requests.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
import logging
13
from typing import Any, Dict, Optional, Union
24

35
import requests
@@ -15,6 +17,8 @@
1517
TransportServerError,
1618
)
1719

20+
log = logging.getLogger(__name__)
21+
1822

1923
class RequestsHTTPTransport(Transport):
2024
""":ref:`Sync Transport <sync_transports>` used to execute GraphQL queries
@@ -135,6 +139,10 @@ def execute( # type: ignore
135139
data_key: payload,
136140
}
137141

142+
# Log the payload
143+
if log.isEnabledFor(logging.INFO):
144+
log.info(">>> %s", json.dumps(payload))
145+
138146
# Pass kwargs to requests post method
139147
post_args.update(self.kwargs)
140148

@@ -144,6 +152,9 @@ def execute( # type: ignore
144152
)
145153
try:
146154
result = response.json()
155+
156+
if log.isEnabledFor(logging.INFO):
157+
log.info("<<< %s", response.text)
147158
except Exception:
148159
# We raise a TransportServerError if the status code is 400 or higher
149160
# We raise a TransportProtocolError in the other cases

0 commit comments

Comments
 (0)