Skip to content

Commit

Permalink
Improve RPC debug logging in async version
Browse files Browse the repository at this point in the history
New way uses jsonrpcclient logging facility to enable full request
logging, not only `args`, also a bit nicer output.
  • Loading branch information
bitphage committed Nov 14, 2019
1 parent a83cdef commit a161e8d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
5 changes: 2 additions & 3 deletions grapheneapi/aio/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Http(Rpc):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.session = aiohttp.ClientSession(loop=kwargs.get("loop"))
self.client = AiohttpClient(self.session, self.url)
enable_debug = True if log.getEffectiveLevel() == logging.DEBUG else False
self.client = AiohttpClient(self.session, self.url, basic_logging=enable_debug)

async def connect(self):
pass
Expand All @@ -31,8 +32,6 @@ async def rpcexec(self, *args):
:param args: args are passed as "params" in json-rpc request:
{"jsonrpc": "2.0", "method": "call", "params": "[x, y, z]"}
"""
log.debug(json.dumps(args))

response = await self.client.request("call", *args)

# Return raw response (jsonrpcclient does own parsing)
Expand Down
4 changes: 4 additions & 0 deletions grapheneapi/aio/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ async def method(*args, **kwargs):
return message

return method

def parse_response(self, *args, **kwargs):
# Disable logging in parent method because we're logging via jsonrpcclient
return super().parse_response(*args, log_on_debug=False, **kwargs)
5 changes: 2 additions & 3 deletions grapheneapi/aio/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def __init__(self, *args, **kwargs):
async def connect(self):
ssl = True if self.url[:3] == "wss" else None
self.ws = await websockets.connect(self.url, ssl=ssl, loop=self.loop)
self.client = WebSocketsClient(self.ws)
enable_debug = True if log.getEffectiveLevel() == logging.DEBUG else False
self.client = WebSocketsClient(self.ws, basic_logging=enable_debug)

async def disconnect(self):
if self.ws:
Expand All @@ -36,8 +37,6 @@ async def rpcexec(self, *args):
if not self.ws:
await self.connect()

log.debug(json.dumps(args))

try:
response = await self.client.request("call", *args)
except ReceivedErrorResponseError as e:
Expand Down
6 changes: 4 additions & 2 deletions grapheneapi/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,16 @@ def connect(self):
def disconnect(self):
pass

def parse_response(self, query):
def parse_response(self, query, log_on_debug=True):
ret = {}
try:
ret = json.loads(query, strict=False)
except ValueError: # pragma: no cover pragma: no branch
raise ValueError("Client returned invalid format. Expected JSON!")

log.debug(json.dumps(query))
# log_on_debug allows to disable logging here even if debug logging is configured
if log_on_debug:
log.debug(json.dumps(query))

if "error" in ret: # pragma: no cover
if "detail" in ret["error"]:
Expand Down

0 comments on commit a161e8d

Please sign in to comment.