Skip to content

Commit

Permalink
Log entire scope and ASGI messages
Browse files Browse the repository at this point in the history
  • Loading branch information
florimondmanca committed Sep 22, 2019
1 parent 410a4fc commit cf3160d
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions httpx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ def get_logger(name: str) -> logging.Logger:
return logging.getLogger(name)


def kv_format(**kwargs: typing.Any) -> str:
"""Format arguments into a key=value line.
>>> formatkv(x=1, name="Bob")
"x=1 name='Bob'"
"""
return " ".join(f"{key}={value!r}" for key, value in kwargs.items())


def get_environment_proxies() -> typing.Dict[str, str]:
"""Gets proxy information from the environment"""

Expand Down Expand Up @@ -247,7 +256,6 @@ def elapsed(self) -> timedelta:
"body": "<{length} bytes>",
"bytes": "<{length} bytes>",
"text": "<{length} chars>",
"headers": "<...>",
}


Expand Down Expand Up @@ -276,15 +284,16 @@ async def __call__(
async def inner_receive() -> dict:
message = await receive()
logged_message = asgi_message_with_placeholders(message)
self.logger.debug(f"sent message={logged_message}")
self.logger.debug(f"sent message={kv_format(**logged_message)}")
return message

async def inner_send(message: dict) -> None:
logged_message = asgi_message_with_placeholders(message)
self.logger.debug(f"received message={logged_message}")
self.logger.debug(f"received {kv_format(**logged_message)}")
await send(message)

self.logger.debug("started")
self.logger.debug(f"started {kv_format(**scope)}")

try:
await self.app(scope, inner_receive, inner_send)
except BaseException as exc:
Expand Down

0 comments on commit cf3160d

Please sign in to comment.