Skip to content

Commit

Permalink
chore(tests): improve logging from provider
Browse files Browse the repository at this point in the history
As the provider is launched in its own Python process, logging is not
configured. So instead of using various `logging` methods, directly
write to `stderr`.

Signed-off-by: JP-Ellis <josh@jpellis.me>
  • Loading branch information
JP-Ellis committed Mar 21, 2024
1 parent 388242d commit 4a7fea8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
4 changes: 2 additions & 2 deletions tests/v3/compatibility_suite/test_v1_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,10 +420,10 @@ def redirect() -> NoReturn:
while True:
if process.stdout:
while line := process.stdout.readline():
logger.debug("Provider stdout: %s", line)
logger.debug("Provider stdout: %s", line.strip())
if process.stderr:
while line := process.stderr.readline():
logger.debug("Provider stderr: %s", line)
logger.debug("Provider stderr: %s", line.strip())

thread = Thread(target=redirect, daemon=True)
thread.start()
Expand Down
30 changes: 14 additions & 16 deletions tests/v3/compatibility_suite/util/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,12 @@ def _add_after_request(self, app: flask.Flask) -> None:

@app.after_request
def log_request(response: flask.Response) -> flask.Response:
logger.debug("Request: %s %s", request.method, request.path)
logger.debug(
"Request query string: %s", request.query_string.decode("utf-8")
)
logger.debug("Request query params: %s", serialize(request.args))
logger.debug("Request headers: %s", serialize(request.headers))
logger.debug("Request body: %s", request.data.decode("utf-8"))
logger.debug("Request form: %s", serialize(request.form))
sys.stderr.write(f"START REQUEST: {request.method} {request.path}\n")
sys.stderr.write(f"Query string: {request.query_string.decode('utf-8')}\n")
sys.stderr.write(f"Header: {serialize(request.headers)}\n")
sys.stderr.write(f"Body: {request.data.decode('utf-8')}\n")
sys.stderr.write(f"Form: {serialize(request.form)}\n")
sys.stderr.write("END REQUEST\n")

with (
self.provider_dir
Expand All @@ -210,13 +208,13 @@ def log_request(response: flask.Response) -> flask.Response:

@app.after_request
def log_response(response: flask.Response) -> flask.Response:
try:
body = response.get_data(as_text=True)
except UnicodeDecodeError:
body = "<binary>"
logger.debug("Response: %s", response.status_code)
logger.debug("Response headers: %s", serialize(response.headers))
logger.debug("Response body: %s", body)
sys.stderr.write(f"START RESPONSE: {response.status_code}\n")
sys.stderr.write(f"Headers: {serialize(response.headers)}\n")
sys.stderr.write(
f"Body: {response.get_data().decode('utf-8', errors='replace')}\n"
)
sys.stderr.write("END RESPONSE\n")

with (
self.provider_dir
/ f"response.{datetime.now(tz=UTC).strftime('%H:%M:%S.%f')}.json"
Expand All @@ -226,7 +224,7 @@ def log_response(response: flask.Response) -> flask.Response:
"status_code": response.status_code,
"headers_list": serialize(response.headers),
"headers_dict": serialize(dict(response.headers)),
"body": body,
"body": response.get_data().decode("utf-8", errors="replace"),
},
f,
)
Expand Down

0 comments on commit 4a7fea8

Please sign in to comment.